使用 Invoke-Command 和 Get-Item cmdlet。
这是我为您整理的一些 PowerShell 3.0 代码。这确实需要 PowerShell 版本 3 才能运行,因此我添加了 #Requires -Version 3 注释来检测任何版本更少。此外,此代码尚未经过测试,但我明天可以这样做并与您联系。
顺便说一句,我不确定 Invoke-Command 的 -Scriptblock 参数是否可以像我在这里所做的那样使用,但我相信有人会插话并对此提供一些说明。
无论如何,这是我散布在一起的:
使用invoke-command 和get-item cmdlet。
这是我为您整理的一些代码,但尚未经过测试。我明天可以测试一下。另外,我不确定是否可以像这样使用 Invoke-Command 的 -Scriptblock 参数来运行所有这些代码。我相信有人会插话并提供一些清晰度。
无论如何,这是我写的,顺便说一下,这需要 PowerShell 版本 3,但我为此投入了 #Requires -Version 3:
需要 - 版本 3
$Domain = (Get-ADDomain).NetBIOSName
$Cred = Get-Credential -Credential "$Domain\Enter User Name"
$Computer = Read-Host "请在此处输入计算机名称:"
Invoke-Command -Credential "$Cred" -Computername "$Computer" -Scriptblock { #检查.Net 4 Framework是否安装#--------------------- - - - - - - - - - - - #地区
Write-Host "Checking to see if .Net Framework 4 is installed. Please wait..." -ForegroundColor "White"
Start-Sleep -Seconds 5
$DotNet4_Reg = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full').Install
If ($DotNet4_Reg -eq "1")
{
Write-Host ".Net 4 Framework is installed on this computer!" -ForegroundColor "White"
""
""
Write-Host "This script will now exit. Please wait..." -ForegroundColor "White"
Start-Sleep -Seconds 5
Exit
}
#EndRegion
#If the .Net 4 Framework is not installed, download and install it
#-----------------------------------------------------------------
#Region
ElseIf ($DotNet4_Reg -ne "1")
{
Write-Warning ".Net Framework 4 is not installed!"
$DotNet4_Inst_Prompt = 0
While ($DotNet4_Inst_Prompt -ne "Y", "y", "N", "n")
{
Write-Host "Please enter either Y or y for Yes, or N or n for No in the following prompt."
""
""
$DotNet4_Inst_Prompt = Read-Host "Would you like to download and install the .Net Framework 4?"
""
""
}
If ($DotNet4_Inst_Prompt -eq "Y", "y")
{
# Download the .Net Framework 4 from: http://www.microsoft.com/downloads/info.aspx?na=41&SrcFamilyId=0A391ABD-25C1-4FC0-919F-B21F31AB88B7&SrcDisplayLang=en&u=http%3a%2f%2fdownload.microsoft.com%2fdownload%2f9%2f5%2fA%2f95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE%2fdotNetFx40_Full_x86_x64.exe
$DotNet4_Dwnld_Src = "http://www.microsoft.com/downloads/info.aspx?na=41&SrcFamilyId=0A391ABD-25C1-4FC0-919F-B21F31AB88B7&SrcDisplayLang=en&u=http%3a%2f%2fdownload.microsoft.com%2fdownload%2f9%2f5%2fA%2f95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE%2fdotNetFx40_Full_x86_x64.exe"
$DotNet4_Dwnld_Dest = "$env:TEMP\dotNetFx40_Full_x86_x64.exe"
$WebClient = New-Object System.Net.WebClient
Try
{
$WebClient.DownloadFile("$DotNet4_Dwnld_Src", "$DotNet4_Dwnld_Dest")
If (Test-Path $DotNet4_Dwnld_Dest)
{
$InstLog = "/Log $env:TEMP\${env:COMPUTERNAME}.htm"
$Arguments = @()
$Arguments += "`"/passive`""
$Arguments += "`"/norestart`""
$Arguments += "`"$InstLog`""
$Inst_DotNet4 = (Start-Process "$DotNet4_Dwnld_Dest" -ArgumentList $Arguments -Wait -Passthru)
#If the exit code is 0, the SCCM 2012 Admin console successfully installed
If ($Inst_DotNet4.ExitCode -eq "0")
{
Write-Host "The .Net Framework 4 installation completed sucessfully!" -ForegroundColor "White"
""
""
Write-Host "This script will now exit. Please wait..." -ForegroundColor "White"
Start-Sleep -Seconds 5
Exit
}
#If the exit code is 3010, the SCCM 2012 Admin console successfully installed, but the system requires a restart
Elseif ($Inst_DotNet4.ExitCode -eq "3010")
{
Write-Host "The .Net Framework 4 installation completed sucessfully!" -ForegroundColor "White"
""
""
Write-Host "Your system requires a restart to complete the .Net Framework 4 installation." -ForegroundColor "White"
""
""
Write-Host "Please enter either "Y" or "y" for Yes, or "N" or "n" for No in the following prompt." -ForegroundColor "White"
""
""
$Inst_DotNet4_Prompt_Restart = 0
While ($Inst_DotNet4_Prompt_Restart -ne "Y", "y", "N", "n")
{
$Inst_DotNet4_Prompt_Restart = Read-Host -Prompt "Would you like to restart your computer now?"
}
If ($Inst_DotNet4_Prompt_Restart -eq "Y", "y")
{
Restart-Computer
}
ElseIf ($Inst_DotNet4_Prompt_Restart -eq "N", "n")
{
Write-Host "You chose not to restart your computer now!" -ForegroundColor "White"
""
""
Write-Host "This script will now exit!" -ForegroundColor "White"
""
""
Start-Sleep -Seconds 5
Exit
}
}
Else
{
Write-Error "Exit code cannot be determined!"
""
""
Write-Host "Please review the log file located here: '$env:COMPUTERNAME.htm'"
""
""
Write-Host "This script will now exit!" -ForegroundColor "White"
Start-Sleep -Seconds 5
Exit
}
}
}
Catch
{
Write-Error "An error occurred during the .Net Framework 4 download and install!"
""
""
$DotNet4_Local_UNC_Path_Prompt = 0
While ($DotNet4_Local_UNC_Path_Prompt -ne "Y", "y", "N", "n")
{
Write-Host "Please enter either "Y" or "y" for Yes, or "N" or "n" for No in the following prompt." -ForegroundColor "White"
""
""
$DotNet4_Local_UNC_Path_Prompt = Read-Host "Would you like to install a local .Net Framework 4 package?"
""
""
}
If ($DotNet4_Local_UNC_Path_Prompt -eq "Y" -or "y")
{
While (!($DotNet4_Local_UNC_Path))
{
$DotNet4_Local_UNC_Path = Read-Host = -Prompt "Please enter the local path or UNC path to the .Net Framework 4 package:"
""
""
}
$InstLog = "/Log $env:TEMP\${env:COMPUTERNAME}.htm"
$Arguments = @()
$Arguments += "`"/passive`""
$Arguments += "`"/norestart`""
$Arguments += "`"$InstLog`""
$Inst_DotNet4 = (Start-Process "$DotNet4_Local_UNC_Path" -ArgumentList $Arguments -Wait -Passthru)
#If the error code is 0, the SCCM 2012 Admin console successfully installed
If ($Inst_DotNet4.ExitCode -eq "0")
{
Write-Host "The .Net Framework 4 installation completed sucessfully!" -ForegroundColor "White"
""
""
Write-Host "This script will now exit. Please wait..." -ForegroundColor "White"
Start-Sleep -Seconds 5
Exit
}
#If the error code is 3010, the SCCM 2012 Admin console successfully installed, but the system requires a restart
Elseif ($Inst_DotNet4.ExitCode -eq "3010")
{
Write-Host "The .Net Framework 4 installation completed sucessfully!" -ForegroundColor "White"
""
""
Write-Host "Your system requires a restart to complete the .Net Framework 4 installation." -ForegroundColor "White"
""
""
Write-Host "Please enter either "Y" or "y" for Yes, or "N" or "n" for No in the following prompt." -ForegroundColor "White"
""
""
$Inst_DotNet4_Prompt_Restart = 0
While ($Inst_DotNet4_Prompt_Restart -ne "Y", "y", "N", "n")
{
$Inst_DotNet4_Prompt_Restart = Read-Host -Prompt "Would you like to restart your computer now?"
}
If ($Inst_DotNet4_Prompt_Restart -eq "Y", "y")
{
Restart-Computer
}
ElseIf ($Inst_DotNet4_Prompt_Restart -eq "N", "n")
{
Write-Host "You chose not to restart your computer now!" -ForegroundColor "White"
""
""
Write-Host "This script will now exit!" -ForegroundColor "White"
""
""
Start-Sleep -Seconds 5
Exit
}
}
Start-Sleep -Seconds 5
}
Write-Warning "Timed out" -ForegroundColor "White"
""
""
Write-Host "The script will now exit!" -ForegroundColor "White"
Start-Sleep -Seconds 5
Exit
}
ElseIf ($DotNet4_Local_UNC_Path_Prompt -eq "N", "n")
{
Write-Warning "You selected not to downlaod the .Net 4 Framework!"
""
""
$DotNet4_Local_UNC_Path_Prompt = 0
While ($DotNet4_Local_UNC_Path_Prompt -ne "Y", "y", "N", "n")
{
Write-Host "Please enter either "Y" or "y" for Yes, or "N" or "n" for No in the following prompt." -ForegroundColor "White"
""
""
$DotNet4_Local_UNC_Path_Prompt = Read-Host "Would you like to install a local .Net 4 package?" -ForegroundColor "White"
""
""
}
If ($DotNet4_Local_UNC_Path_Prompt -eq "Y" -or "y")
{
$DotNet4_Local_UNC_Path_Prompt = 0
While ($DotNet4_Local_UNC_Path_Prompt -ne "Y", "y", "N", "n")
{
Write-Host "Please enter either "Y" or "y" for Yes, or "N" or "n" for No in the following prompt." -ForegroundColor "White"
""
""
$DotNet4_Local_UNC_Path_Prompt = Read-Host "Would you like to install a local .Net Framework 4 package?"
""
""
}
If ($DotNet4_Local_UNC_Path_Prompt -eq "Y" -or "y")
{
$DotNet4_Local_UNC_Path = 0
While (!($DotNet4_Local_UNC_Path))
{
$DotNet4_Local_UNC_Path = Read-Host = -Prompt "Please enter the local path or UNC path to the .Net Framework 4 package:"
""
""
}
$InstLog = "/Log $env:TEMP\${env:COMPUTERNAME}.htm"
$Arguments = @()
$Arguments += "`"/passive`""
$Arguments += "`"/norestart`""
$Arguments += "`"$InstLog`""
$Inst_DotNet4 = (Start-Process "$DotNet4_Local_UNC_Path" -ArgumentList $Arguments -Wait -Passthru)
#If the error code is 0, the SCCM 2012 Admin console successfully installed
If ($Inst_DotNet4.ExitCode -eq "0")
{
Write-Host "The .Net Framework 4 installation completed sucessfully!" -ForegroundColor "White"
""
""
Write-Host "This script will now exit. Please wait..." -ForegroundColor "White"
Start-Sleep -Seconds 5
Exit
}
#If the error code is 3010, the SCCM 2012 Admin console successfully installed, but the system requires a restart
Elseif ($Inst_DotNet4.ExitCode -eq "3010")
{
Write-Host "The .Net Framework 4 installation completed sucessfully!" -ForegroundColor "White"
""
""
Write-Host "Your system requires a restart to complete the .Net Framework 4 installation." -ForegroundColor "White"
""
""
Write-Host "Please enter either "Y" or "y" for Yes, or "N" or "n" for No in the following prompt." -ForegroundColor "White"
""
""
$Inst_DotNet4_Prompt_Restart = 0
While ($Inst_DotNet4_Prompt_Restart -ne "Y", "y", "N", "n")
{
$Inst_DotNet4_Prompt_Restart = Read-Host -Prompt "Would you like to restart your computer now?"
}
If ($Inst_DotNet4_Prompt_Restart -eq "Y", "y")
{
Restart-Computer
}
ElseIf ($Inst_DotNet4_Prompt_Restart -eq "N", "n")
{
Write-Host "You chose not to restart your computer now!" -ForegroundColor "White"
""
""
Write-Host "This script will now exit!" -ForegroundColor "White"
""
""
Start-Sleep -Seconds 5
Exit
}
}
}
}
}
}
}
}