简体   繁体   English

传递凭据以使用PowerShell在远程计算机上运行批处理脚本

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

I've been working on a script that will loop through a list of computers, uninstall old AV software if it's present and install our new AV software if it's not present. 我一直在研究一个脚本,该脚本将遍历计算机列表,如果存在,请卸载旧的AV软件,如果不存在,请安装新的AV软件。 The script does this by executing two files: one is a batch script (called Sophos-Installation.bat) that will check for the presence of the old AV software, and run our new AV vendor's competitor removal tool. 该脚本通过执行两个文件来执行此操作:一个是批处理脚本(称为Sophos-Installation.bat),该脚本将检查是否存在旧的AV软件,并运行新的AV供应商的竞争对手删除工具。 Once that is done the batch script will silently launch the executable to install our new AV software. 完成后,批处理脚本将自动启动可执行文件以安装我们的新AV软件。 The other file will only remove the old AV software, because I have some computers in my environment that have both. 另一个文件将仅删除旧的AV软件,因为我的环境中有一些计算机同时具有这两种软件。 Here is the script: 这是脚本:

#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}

And I have another script that I've been running that copies the necessary files to the remote computer before launching the above script and logging the computer name in a file to my PC. 而且我正在运行另一个脚本,该脚本在启动上述脚本并将文件中的计算机名称记录到我的PC之前,将必要的文件复制到远程计算机。 (unsuccessfully right now. I get an access denied error when the script tries to write to the .txt files. I'm less concerned about that right now but if someone wants to take a whack at that as well feel free.) Here is that script: (目前未成功。当脚本尝试写入.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'}
}

When running this script, the files copy over properly, the batch file executes (the batch file also creates a log on the remote machine when it is run, so I know it's executing) but the old AV isn't uninstalled and the new AV isn't installed. 运行此脚本时,文件会正确复制,批处理文件会执行(批处理文件在运行时也会在远程计算机上创建日志,因此我知道它正在执行),但是旧的AV未卸载且新的AV未安装。 When I remoted into one of my test computers, I manually ran the Sophos-Installation.bat file and got the following error: 当我远程进入一台测试计算机时,我手动运行了Sophos-Installation.bat文件并得到以下错误:

Access is denied. 访问被拒绝。 Access is denied. 访问被拒绝。 The username or password is incorrect. 用户名或密码不正确。

I was then prompted for my admin credentials, and then the batch file worked as intended. 然后提示我输入管理员凭据,然后该批处理文件按预期工作。 So clearly I need to pass my admin credentials to these machines in order to get this to work. 所以很明显,我需要将我的管理员凭据传递给这些计算机,以使其正常工作。 Every other time I've tried to pass credentials in PowerShell it hasn't worked properly so I'm hoping someone on here can tell me the following: 每当我尝试在PowerShell中传递凭据时,它都无法正常工作,因此我希望有人可以告诉我以下内容:

  • How do I setup a credential variable that will work 如何设置将起作用的凭据变量
  • Where in the above scripts do I need to implement that credential variable. 在上面的脚本中,我需要实现该凭证变量。
  • Any other information I might need to know to get this working properly. 我可能需要知道的其他信息才能使它正常工作。

Please keep in mind when answering that I'm a beginner in PowerShell (and IT in general, this is my first IT job), I took a class about 6 weeks ago and this is my first project that will implement it. 在回答我是PowerShell的初学者(通常是IT方面,这是我的第一份IT工作)时,请记住这一点,大约6周前我上了课,这是实现它的我的第一个项目。 Thanks in advance for your help! 在此先感谢您的帮助!

Update: I've added a credential variable to my script. 更新:我在脚本中添加了凭据变量。 I've implemented it in 3 places and I'm still getting prompted for credentials on my end. 我已经在3个地方实现了它,但仍然会提示我输入凭据。 Here are my updated scripts: 这是我更新的脚本:

#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}

and: 和:

    #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'}
    }

While the command to create the secure string file isn't in the script I did create it. 虽然脚本中没有创建安全字符串文件的命令,但我确实创建了它。 I've used the credential parameter with Invoke-Command, and the two Start-Process commands, but I'm still being prompted for a username and password. 我在Invoke-Command和两个Start-Process命令中使用了凭证参数,但是仍然提示我输入用户名和密码。

Can you please test the below code I have added a credential 能否请您测试以下代码,我已添加凭据

$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