如何让PowerShell使用脚本上传递的参数成功运行?

时间:2017-04-23 23:05:49

标签: powershell

我编写了一个远程更新计算机的脚本,目前正在将脚本本身的用户名,密码和路径作为参数传递,但它仍然提示我输入用户名和密码以连接到远程计算机。我哪里错了?

    [string][ValidateNotNullOrEmpty()]$credentials=$args[0]
    [string][ValidateNotNullOrEmpty()]$password=$args[1]
    $destination =$args[2] 
    $destinationHome =$args[3]
    $sourcePathBat = $args[4] 
    $sourcePathZip = $args[5]
    $command = "cmd.exe /c \\192.168.50.23\deployment\deploy-smartcheck-kiosk.bat upgrade"
    $secPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
    $Params = New-Object System.management.automation.PSCredential($credentials, $secPassword)

    Write-Host $args[0]
    Write-Host $args[1]
    Write-Host $args[2]
    Write-Host $args[3]
    Write-Host $args[4]
    Write-Host $args[5]

    net use $destination $secPassword /USER:$credentials

    Copy-Item -Path $sourcePathBat -Destination $destinationHome;

    Copy-Item -Path $sourcePathZip -Destination $destinationHome;

    Write-Host "Performing remote command..."
    Write-Host $command

    Invoke-Command -ComputerName 192.168.50.23 -Credential $Params -ErrorAction Stop -ScriptBlock {
    param($command)
    Write-Host "Trying to execute remote command ($command)"
    Invoke-Expression -Command: "$command"
    } -ArgumentList $command

Read-Host -Prompt"按Enter退出..."

更新了脚本并进行了一些更改

这就是我目前正在运行脚本的方式:         &安培; '。\ execute-copy-and-command - Copy.ps1' test test2 test3 test4

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

  1. [PSCredential]对象需要密码为securesctring ...这里$ args 1的类型应为System.Security.SecureString。
    1. Enter-PSSession用于交互式PowerShell会话,在脚本中使用它没有意义。无论你在此之后保留什么都不会在远程机器(PS V 2)中运行,如果正在运行将在本地机器上运行。
  2. 您可以查看here

    此致

    Kvprasoon

相关问题