Powershell remoting and session management


hi,

i new powershell , need use inside java program. want connect remote machines using powershell , invoke commands. using invoke-command cmdlet , working, problem every command need create new session, not efficient. using following script invoking remote commands.

param([string]$ip="", [string]$username="", [string]$password="", [string]$cmd)    if($ip -eq "") {      $ip = "0.0.0.0"  }    if($username -eq "") {      $username = "xyz"  }    if($password -eq "") {      $password = "xyz"  }    $s = new-pssession  if(($s -eq $null -or $s -eq "") -and !$cmd) {      $securepassword = $password | convertto-securestring -asplaintext -force;      $mycred = new-object -typename system.management.automation.pscredential($username, $securepassword);      $session = new-pssession -computername $ip -credential $mycred;      write-host "session created.";  } else {      write-host "session exists.";  }    if($cmd) {      invoke-command -session $session -scriptblock {$cmd};      write-host "invoke-command done.";  }

i passing $ip, $username, $password , $cmd outside script.it showing following error.

ps c:\windows\system32> get-process  session exists.    invoke-command : cannot validate argument on parameter 'session'. argument   null or empty. supply argument not null or empty , try   command again.  @ c:\session.ps1:28 char:28  +     invoke-command -session <<<<  $session -scriptblock {$cmd};      + categoryinfo          : invaliddata: (:) [invoke-command], parameterbind      ingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,microsoft.power      shell.commands.invokecommandcommand     invoke-command done.  

please me out have limited knowledge on powershell.

have tried without -session, i.e.

invoke-command $session ...



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Edit Group Policy

Hyper-V VM not reaching OS 'Logon' screen

DNS question...