我正在创建一个批处理文件来检查特定的 dotnet 版本,如果在远程计算机上找不到则进行安装。
目前我正在使用 psexec.exe 在远程计算机上执行命令。
用它来检查 .net 版本。 reg query "HKLM\Software\Microsoft\NET Framework Setup\NDP" /s /v 版本 | findstr /i 版本 |排序 /+26 /r
我需要制作一个批处理文件并检查是否安装了 dotnet 4.5 版本,如果没有找到则安装
有人可以帮忙吗?
执行了以下脚本
我正在远程计算机上使用 power shell 调用以下脚本。
$vers = reg query "HKLM\Software\Microsoft\NET Framework Setup\NDP" /s /v version | findstr /i version | sort /+26
$VC="Version REG_SZ 4.5.*"
$dotnetInstalled = "False";
foreach ($v in $vers)
{
if ($v -match $VC)
{
$dotnetInstalled = "True"
}
}
if($dotnetInstalled -match "False")
{
Start-Process 'E:\dotNetFx45_Full_x86_x64.exe' -ArgumentList "/s" -Wait
}
现在我可以看到 dotNetFx45_Full_x86_x64.exe 正在我的任务栏上运行,但进程没有竞争。 (等了1个多小时)
这可能是什么问题?
此示例不需要
psexec
,只需将 \\machine
替换为所需的机器名称即可。
CALL :CHECK_NET_VER "2.0.50727.5420"
IF ERRORLEVEL 1 ECHO .NET 2.0.50727.5420 not found!
CALL :CHECK_NET_VER "1.0.00000.0000"
IF ERRORLEVEL 1 ECHO .NET 1.0.00000.0000 not found!
GOTO :EOF
:CHECK_NET_VER
FOR /F "TOKENS=*" %%R IN ('REG QUERY "\\machine\HKLM\Software\Microsoft\NET Framework Setup\NDP"') DO FOR /F "TOKENS=3 SKIP=1" %%V IN ('REG QUERY "%%R" /V VERSION') DO IF "%%V" == "%~1" TYPE NUL&GOTO :EOF
TYPE 2>NUL
GOTO :EOF
您的脚本的问题是您为安装程序使用了错误的开关。你应该使用“/q /norestart”代替。 如果启用了 Windows UAC,您可能会遇到管理权限问题,因此请调用 RunAs 谓词。
Start-Process -FilePath "\\fileserver\share\dotNetFx45_Full_x86_x64.exe" -ArgumentList "/q /norestart" -Wait -Verb RunAs
如果我在你那里,我不会将整个脚本传递到远程服务器,而是在本地运行代码,并使用 Invoke-Command 来检查 .Net 版本或生成安装程序。
使用 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:
#Requires -Version 3
$Domain = (Get-ADDomain).NetBIOSName
$Cred = Get-Credential -Credential "$Domain\Enter User Name"
$Computer = Read-Host "Please enter the computer name here:"
Invoke-Command -Credential "$Cred" -Computername "$Computer" -Scriptblock { #Check if the .Net 4 Framework is installed
#------------------------------------------
#Region
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
}
}
}
}
}
}
}
}