1

我正在创建一个批处理文件来检查特定的 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 正在我的任务栏上运行,但进程没有竞争。(等了一个多小时)

这可能是什么问题?

4

3 回答 3

0

使用 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
                        }
                    }
                }
            }
        }
    }
}

}

于 2013-11-06T05:52:21.757 回答
0

此示例不需要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
于 2013-07-26T10:56:23.667 回答
0

您的脚本的问题是您为安装程序使用了错误的开关。您应该改用“/q /norestart”。如果启用了 Windows UAC,您可能会遇到管理权限问题,因此调用 RunAs 动词。

Start-Process -FilePath "\\fileserver\share\dotNetFx45_Full_x86_x64.exe" -ArgumentList "/q /norestart" -Wait -Verb RunAs

如果我在哪里,而不是将整个脚本传递给远程服务器,我会在本地运行代码,并使用 Invoke-Command 检查 .Net 版本生成安装程序。

于 2014-02-18T09:38:55.387 回答