0

I am beginner with PowerShell and struggling to get this around with the help from different sites, My requirement and scenario is

I have a windows server 2008(rktdepy) with PowerShell installed and I have packaged application with a .cmd file. When I click this .cmd file the application will be deployed.

The server name is rktdepy and I want to create a PowerShell script which will connect to other servers in the network (the server names should be picked up from a txt files) and install the application accessing the file remotely from rktdepy server. The files are not supposed to be copied to any server and should not use psxec for security reason.

So far I have used invoke and mapping the network drive but still I have issues

$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"}

The above script throws error,

I dont want to use any password in the script and it should fetch the current logged in user password from rktdepy server. I is ok if the scripts prompts for a user name and password which will have admin access to all servers.

2
  • 2
    What error is being thrown? You can't say that an error is thrown and then make people guess at it. Commented Aug 9, 2013 at 16:02
  • Sorry i missed to mention this, I had previous error related to limited session which i got it sorted out by increasing the session. BUt the problem is that i dont receive any error message and when i check login into the server, i couldnt see the drives mapped until i logoff and log back in. Moreover for testing purpose i just mentioned to create a folder in .cmd but i dont see any folder been created Commented Aug 10, 2013 at 10:01

2 Answers 2

1

It looks like you are dealing with a couple problems. One is that the session where you map the drive is gone when you run the next Invoke-Command that uses the mapped drive. You could move that into the same script block to fix a problem like that. The second one is a "second hop" issue. See a resource like Don Jones' Secrets of PowerShell Remoting free ebook on http://powershell.org/wp/books. Steve

Sign up to request clarification or add additional context in comments.

Comments

0

I have testing the following on my machine and it is working so far. There is also another method you can try out listed below.

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.

3 Comments

Thank you very much for your valuable inputs and extremely sorry for the delay as i was long leave... I tried the script and it works fabulously if i have to run a normal batch file with creating folders or files.... but it was my mistake i missed to mention this earlier... the script should execute a .cmd file rather than .bat file. The .cmd file has got its own scripts which uses cscript also.. please advice
Please mark this as answer if it solved your problem and ask another question related to it.
I have asked as a separate question, can you please kindly help me with that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.