挖土

Coding for fun.
posts - 29, comments - 20, trackbacks - 0, articles - 23
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

2010年12月8日

DotNet Framework 4.0 发布以后,你在 PowerShell 中执行引用 4.0 的 exe、dll,会报错,说你的Powershell的 dotnet framework 版本和你引用的 assemblis 不一致。这是因为默认PowerShell 的 Notdet FrameWork 版本是 v2.0, 我们需要让 PowserShell 支持 v4.0。


你只要创建一个 Powershell.exe.config 文件,在以下两个目录,就可以了。

  • C:\Windows\System32\WindowsPowerShell\v1.0
  • C:\Windows\SysWOW64\WindowsPowerShell\v1.0
1 <?xml version="1.0"?>
2 <configuration>
3     <startup useLegacyV2RuntimeActivationPolicy="true">
4         <supportedRuntime version="v4.0.30319"/>
5         <supportedRuntime version="v2.0.50727"/>
6     </startup>
7 </configuration>

 

 

 另外,也可以更改注册表, 把 RuntimeVersionv2.0.50727 改成  v4.0.30319。

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine

 

 

posted @ 2010-12-08 10:37 挖土. 阅读(63) 评论(0) 编辑

在微软的技术论坛上看到这个脚本,感觉自己的PowerShell 理念,还有很多不熟悉,好好研究了一下这个脚本,记录在自己的Blog上。


怎样同时为网络中很多电脑安装Dotnet Framework 4.0。

 

 1 $Hosts= Get-Content -Path "C:\hosts.txt"
 2 [ScriptBlock] $script = {
 3     #$ErrorActionPreference = "Stop"
 4     $MachineName= gc env:computername
 5     C:\Windows\System32\net.exe use "\\RemoteMachine\SharedFolder\DotnetFrameWork 4.0" password /user:domain\username 
 6     if(!(Test-Path "C:\Temp"))
 7     {
 8         New-Item -Path "C:\Temp" -ItemType directory
 9     }
10     Copy-Item -Path "\\RemoteMachine\SharedFolder\DotnetFrameWork 4.0\dotNetFx40_Full_x86_x64.exe" -Destination "C:\Temp" -Force -Recurse    
11     function LaunchProcessAndWait([string] $FileName, [string] $CommandLineArgs)
12     {
13         #preferences
14         $ErrorActionPreference="Stop"
15         try
16         {
17             Write-Host "Starting process $FileName on machine $($MachineName)"
18             $p = Start-Process -FilePath $FileName -ArgumentList $CommandLineArgs -Wait -PassThru
19             Write-Host "The process $FileName exit code is $($p.exitcode)"
20             return $p.ExitCode            
21         }
22         catch
23         {
24             Write-Host "Error launching process $FileName"
25             throw
26         }
27     }
28     LaunchProcessAndWait "C:\Temp\dotNetFx40_Full_x86_x64.exe" "/I /q"
29 }
30 $cred = Get-Credential
31 $sessions = $Hosts | New-PSSession -Credential $cred
32 Invoke-Command -ScriptBlock $script -Session $sessions
33 $sessions | Remove-PSSession

 

 

另外的解决方法: 

-CredSSP 非常重要. 否则安装会被远程机的 UNC 阻止而失败,需要为所有的远程机启用 CredSSP. This works only from Vista SP1 onwards since CredSSP is not available on XP SP3.

 

代码
1 $cred = Get-Credential
2 Invoke-Command -ComputerName "Server01" -ScriptBlock { Start-Process -FilePath "\\Staging\Share\dotNetFx40_Full_x86_x64.exe" -ArgumentsList "/q /norestart" } -CredSSP -Crdential $cred

 

 

 

posted @ 2010-12-08 10:09 挖土. 阅读(40) 评论(0) 编辑