Click here to Skip to main content
969 members (118 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4K views
Posted 18 Apr 2012

PowerShell ASP: Using Invoke-Command with New-PSSession

, 18 Apr 2012
Rate this:
Please Sign up or sign in to vote.
In order to use the Invoke -Command with the New-PSSession cmdlet you will need to have the proper credentials set.

In order to use the Invoke -Command with the New-PSSession cmdlet you will need to have the proper credentials set.

The credential parameter in Powershell taks a PSCredential object. Normally you would prompt a user to enter the information in a dialog and then save the response as a PSCredential object using the Get-Credential cmdlet. Of course in PowerShell ASP that's not an option. We can get around this by just setting the credentials manually in a PowerShell script:

$securePassword = ConvertTo-SecureString "Password" -AsPlainText -force 
$credential = New-Object System.Management.Automation.PsCredential("domain\username",$securePassword)

After speicfiying your credentails you can then use them in your call to create a New-PSSession Using the PowerShell script below.

$session = New-PSSession -computername hostname -credential $cred

Below is an example of a complete PowerShell ASP page:

<html>
	<title>Test</title> 
	<body> 
	<pre> 
	<% 
		$securePassword = ConvertTo-SecureString "Password" -AsPlainText -force 
		$credential = New-Object System.Management.Automation.PsCredential("domain\username",$securePassword)  
		$session = New-PSSession -computername hostname -credential $cred 
		$command = {ls} 
		$res = Invoke-Command -session $session -scriptblock $command 
		foreach($item in $res){ 	
			Write-Host("Mode: " + $item.Mode) 
			Write-Host("Last Write Time: " + $item.LastWriteTime) 
			Write-Host("Length: " + $item.Length) 
			Write-Host("Name: " + $item.Name) 
			Write-Host("<hr/>") 
		}	 

	%> 
	</pre> 
	</body> 
</html>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

powershellscripter
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Organisation (No members)



You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
RA-Web1 | 2.8.170924.2 | Last Updated 18 Apr 2012
Article Copyright 2012 by powershellscripter
Everything else Copyright © RootAdmin, 1999-2017
Layout: fixed | fluid