挖土

Coding for fun.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用 Powershell 安装Dotnet FrameWorrk 4.0

Posted on 2010-12-08 10:09  挖土.  阅读(665)  评论(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