0

我是 PowerShell 的初学者,在不同站点的帮助下努力解决这个问题,我的要求和场景是

我有一个安装了 PowerShell 的 Windows Server 2008(rktdepy),并且我已经用一个.cmd文件打包了应用程序。当我单击此.cmd文件时,将部署应用程序。

服务器名称是rktdepy,我想创建一个 PowerShell 脚本,它将连接到网络中的其他服务器(服务器名称应从 txt 文件中获取)并安装从 rktdepy服务器远程访问文件的应用程序。这些文件不应被复制到任何服务器,并且出于安全原因不应使用 psxec。

到目前为止,我已经使用了调用和映射网络驱动器,但我仍然有问题

$Comsession = Get-content c:\adminfiles\scripts\deploy.txt | new-pssession -throttlelimit 50
Invoke-command -computername RKTDEPLY54 -scriptblock { (new-object -comobject wscript.network).mapnetworkdrive("R:", "\\rktdepy\deploy", $true) }
Invoke-command -session $comsession -scriptblock {"CMD /C r:\QR_DEPLOY.CMD"}

上面的脚本抛出错误,

我不想在脚本中使用任何密码,它应该从rktdepy服务器获取当前登录的用户密码。如果脚本提示输入对所有服务器具有管理员访问权限的用户名和密码,我可以。

4

2 回答 2

1

看起来你正在处理几个问题。一个是当您运行下一个使用映射驱动器的 Invoke-Command 时,映射驱动器的会话消失了。您可以将其移动到同一个脚本块中以解决此类问题。第二个是“第二跳”问题。在http://powershell.org/wp/books上查看 Don Jones 的 PowerShell Remoting 的秘密免费电子书等资源。史蒂夫

于 2013-08-20T21:37:59.697 回答
0

我在我的机器上测试了以下内容,到目前为止它正在工作。您还可以尝试下面列出的另一种方法。

Method1:
1. I have txt file with a list of computers named allcomputers.txt. It contains name of machines on each line.

Machine10  
Machine20  
Machine30  
Machine40  
  1. The deployment script (mydeploytest.ps1) which accepts Computername, Username and Password as input and creates a new PSSession and then invokes command.

    param(
    [string]$ComputerName,
    [string]$User,
    [string]$pass
    )
    Get-PSSEssion | Remove-PSSession
    $session = New-PSSession -ComputerName $ComputerName
    Invoke-Command -Session $session -ScriptBlock {
    param(
    [string]$ComputerName,
    [string]$Username,
    [string]$Password
    )
    $net = new-object -ComObject WScript.Network
    $net.MapNetworkDrive("U:", "\\RKTDEPY\deploy", $false, $Username, $Password)
    Invoke-Expression "CMD /C U:\deploy.cmd"
    $net.RemoveNetworkDrive("U:")
    } -args $ComputerName,$User,$pass
    Get-PSSEssion | Remove-PSSession

  2. Powershell commandline oneline to accomplish deployment task.

PS C:> Get-Content C:\scripts\allcomputers.txt | Foreach { C:\scripts\mydeploytest.ps1 $_ "yourserviceaccount" "password"}

Method2:
The help method for Invoke-Command has an example on how to solve the doublehop issue stevals is mentioning in the answer.

PS C:\> Enable-WSManCredSSP -Delegate Server02
 PS C:\>Connect-WSMan Server02
 PS C:\>Set-Item WSMan:\Server02*\Service\Auth\CredSSP -Value $true
 PS C:\>$s = New-PSSession Server02
 PS C:\>Invoke-Command -Session $s -ScriptBlock {Get-Item \\Net03\Scripts\LogFiles.ps1} -Authentication CredSSP
 -Credential Domain01\Admin01

I think with little modification to method 2 you can achieve what you want.

于 2013-08-21T15:45:43.787 回答