簡體   English   中英

傳遞憑據以使用PowerShell在遠程計算機上運行批處理腳本

[英]Pass credentials to run batch script on remote computer with PowerShell

我一直在研究一個腳本,該腳本將遍歷計算機列表,如果存在,請卸載舊的AV軟件,如果不存在,請安裝新的AV軟件。 該腳本通過執行兩個文件來執行此操作:一個是批處理腳本(稱為Sophos-Installation.bat),該腳本將檢查是否存在舊的AV軟件,並運行新的AV供應商的競爭對手刪除工具。 完成后,批處理腳本將自動啟動可執行文件以安裝我們的新AV軟件。 另一個文件將僅刪除舊的AV軟件,因為我的環境中有一些計算機同時具有這兩種軟件。 這是腳本:

#Variables that store the path to 32-bit and 64-bit versions of Sophos
$Sophos64 = Test-Path 'C:\Program Files\Sophos'
$Sophos32 = Test-Path 'C:\Program Files (x86)\Sophos'

#Variables that store the path to 32-bit and 64-bit versions of Kaspersky
$Kaspersky64 = Test-Path 'C:\Program Files\Kaspersky Lab'
$Kaspersky32 = Test-Path 'C:\Program Files (x86)\Kaspersky Lab'

#The following block will run the Sophos-Installation batch file, removing any instance of Kaspersky and installing Sophos
      If ($Sophos64 -eq $false -or $Sophos32 -eq $false) 
      {
      Start-Process -FilePath C:\Temp\AVInstall\Sophos-Installation.bat
      $env:COMPUTERNAME |Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Sophos_Installed.txt" -Append
      }
#The following block will remove Kaspersky from a machine if it is present and Sophos is already installed.
        Elseif (($Kaspersky64 -eq $true -or $Kaspersky32 -eq $true) -and ($Sophos64 -eq $true -or $Sophos32 -eq $true)) 
        {
        Start-Process -FilePath C:\Temp\AVInstall\AVRemoveW.exe
        $env:COMPUTERNAME | Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Kaspersky_Removed.txt" -Append
        }
#The last block will only be executed if Sophos is installed and Kaspersky isn't, and makes no changes to the machine.
        else {$env:COMPUTERNAME | Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Proper_Configuration.txt" -Append}

而且我正在運行另一個腳本,該腳本在啟動上述腳本並將文件中的計算機名稱記錄到我的PC之前,將必要的文件復制到遠程計算機。 (目前未成功。當腳本嘗試寫入.txt文件時,我收到訪問被拒絕的錯誤。我現在不太擔心這一點,但是如果有人想對此大肆抨擊,請放心。)該腳本:

    $computers = Get-Content -Path 'C:\Sophos Results\TestGroup.txt'
ForEach ($computer in $computers)
{
$connection = Test-Connection -ComputerName $computer -Quiet
if ($connection = $true)
{
try{
Copy-Item -Path C:\Temp\AVInstall -Destination "\\$computer\c$\Temp\AVInstall" -Recurse -Force
Invoke-Command -ComputerName $computer -FilePath 'C:\Sophos Results\InstallSophos_UninstallKaspersky_Test.ps1'
echo $computer | Out-File 'C:\Sophos Results\Script_Successful.txt' -Append
}
catch {
echo $computer | Out-File -FilePath 'C:\Sophos Results\Script_Failed.txt' -Append
}
}
else {echo $computer | Out-File -FilePath 'C:\Sophos Results\UnreachableComputers.txt'}
}

運行此腳本時,文件會正確復制,批處理文件會執行(批處理文件在運行時也會在遠程計算機上創建日志,因此我知道它正在執行),但是舊的AV未卸載且新的AV未安裝。 當我遠程進入一台測試計算機時,我手動運行了Sophos-Installation.bat文件並得到以下錯誤:

訪問被拒絕。 訪問被拒絕。 用戶名或密碼不正確。

然后提示我輸入管理員憑據,然后該批處理文件按預期工作。 所以很明顯,我需要將我的管理員憑據傳遞給這些計算機,以使其正常工作。 每當我嘗試在PowerShell中傳遞憑據時,它都無法正常工作,因此我希望有人可以告訴我以下內容:

  • 如何設置將起作用的憑據變量
  • 在上面的腳本中,我需要實現該憑證變量。
  • 我可能需要知道的其他信息才能使它正常工作。

在回答我是PowerShell的初學者(通常是IT方面,這是我的第一份IT工作)時,請記住這一點,大約6周前我上了課,這是實現它的我的第一個項目。 在此先感謝您的幫助!

更新:我在腳本中添加了憑據變量。 我已經在3個地方實現了它,但仍然會提示我輸入憑據。 這是我更新的腳本:

#Variables that store the path to 32-bit and 64-bit versions of Sophos
$Sophos64 = Test-Path 'C:\Program Files\Sophos'
$Sophos32 = Test-Path 'C:\Program Files (x86)\Sophos'

#Variables that store the path to 32-bit and 64-bit versions of Kaspersky
$Kaspersky64 = Test-Path 'C:\Program Files\Kaspersky Lab'
$Kaspersky32 = Test-Path 'C:\Program Files (x86)\Kaspersky Lab'

#The following block will run the CCOB-Sophos-Installation batch file, removing any instance of Kaspersky and installing Sophos
      If ($Sophos64 -eq $false -or $Sophos32 -eq $false) 
      {
      Start-Process -FilePath C:\Temp\AVInstall\CCOB-Sophos-Installation.bat -Credential $cred 
      $env:COMPUTERNAME |Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Sophos_Installed.txt" -Append
      }
#The following block will remove Kaspersky from a machine if it is present and Sophos is already installed.
        Elseif (($Kaspersky64 -eq $true -or $Kaspersky32 -eq $true) -and ($Sophos64 -eq $true -or $Sophos32 -eq $true)) 
        {
        Start-Process -FilePath C:\Temp\AVInstall\AVRemoveW.exe -Credential $cred
        $env:COMPUTERNAME | Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Kaspersky_Removed.txt" -Append
        }
#The last block will only be executed if Sophos is installed and Kaspersky isn't, and makes no changes to the machine.
        else {$env:COMPUTERNAME | Out-File -FilePath "\\CCIT-4BXZPN2LP\c$\Sophos Results\Proper_Configuration.txt" -Append}

和:

    #credentials
$username = "mydomain\myusername"
$password = Get-Content -Path 'C:\Sophos Results\mysecurestring.txt' | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password

    $computers = Get-Content -Path 'C:\Sophos Results\TestGroup.txt'
    ForEach ($computer in $computers)
    {
    $connection = Test-Connection -ComputerName $computer -Quiet
    if ($connection -eq $true)
    {
    try{
    Copy-Item -Path C:\Temp\AVInstall -Destination "\\$computer\c$\Temp\AVInstall" -Recurse -Force 
    Invoke-Command -ComputerName $computer -FilePath 'C:\Sophos Results\InstallSophos_UninstallKaspersky_Test.ps1' -Credential $cred
    echo $computer | Out-File 'C:\Sophos Results\Script_Successful.txt' -Append
    }
    catch {
    echo $computer | Out-File -FilePath 'C:\Sophos Results\Script_Failed.txt' -Append
    }
    }
    else {echo $computer | Out-File -FilePath 'C:\Sophos Results\UnreachableComputers.txt'}
    }

雖然腳本中沒有創建安全字符串文件的命令,但我確實創建了它。 我在Invoke-Command和兩個Start-Process命令中使用了憑證參數,但是仍然提示我輸入用戶名和密碼。

能否請您測試以下代碼,我已添加憑據

$Username = "administrator"
 $Password  = ConvertTo-SecureString 'Administrator password here' -AsPlainText -Force

     $Credentials  = New-Object System.Management.Automation.PSCredential $Username, $Password

    $computers = Get-Content -Path 'C:\Sophos Results\TestGroup.txt'
    ForEach ($computer in $computers)
    {
    $connection = Test-Connection -ComputerName $computer -Quiet
    if ($connection = $true)
    {
    try{
    Copy-Item -Path C:\Temp\AVInstall -Destination "\\$computer\c$\Temp\AVInstall" -Recurse -Force -Credential $Credentials
    Invoke-Command -ComputerName $computer -FilePath 'C:\Sophos Results\InstallSophos_UninstallKaspersky_Test.ps1' -Credential $Credentials
    echo $computer | Out-File 'C:\Sophos Results\Script_Successful.txt' -Append
    }
    catch {
    echo $computer | Out-File -FilePath 'C:\Sophos Results\Script_Failed.txt' -Append
    }
    }
    else {echo $computer | Out-File -FilePath 'C:\Sophos Results\UnreachableComputers.txt'}
    } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM