Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Passing credential object - what's the type?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-08-2008   #1 (permalink)
bobuva
Guest


 

Passing credential object - what's the type?

I'm writing a script function that will take a credential object and
assign the username and password to members of a ProcessStartInfo
object. Here's the function:

function Impersonate ([System.Management.Automation.PSCredential]
$credentials, [string] $command, [string] $arguments, [string]
$workingDirectory )
{
$StartInfo = new-object Diagnostics.ProcessStartInfo
$StartInfo.UserName = $credentials.GetNetworkCredential().UserName
$StartInfo.Password = $credentials.GetNetworkCredential().Password
$StartInfo.FileName = $command
$StartInfo.Arguments = $arguments
$StartInfo.WorkingDirectory = $workingDirectory
$StartInfo.LoadUserProfile = $true
$StartInfo.UseShellExecute = $false
[System.Diagnostics.Process]::Start($StartInfo)
}

to test it I'm doing this:

$cred = Get-Credential

Impersonate [System.Management.Automation.PSCredential]$cred, "e:
\toolshed\reflector.exe", "e:\dev\MyOperations.dll", "e:\dev"


I get a runtime error "Cannot convert "System.Object[]" to
"System.Management.Automation.PSCredential" on the line where I call
Impersonate(). Do I have the wrong type for what get-credential
returns?

thx,
bob

My System SpecsSystem Spec
Old 05-08-2008   #2 (permalink)
Oisin (x0n) Grehan [MVP]
Guest


 

Re: Passing credential object - what's the type?

On May 8, 6:26*pm, bobuva <robert...@xxxxxx> wrote:
Quote:

> I'm writing a script function that will take a credential object and
> assign the username and password to members of a ProcessStartInfo
> object. Here's the function:
>
> function Impersonate ([System.Management.Automation.PSCredential]
> $credentials, [string] $command, [string] $arguments, [string]
> $workingDirectory )
> {
> * * * * $StartInfo = new-object Diagnostics.ProcessStartInfo
> * * * * $StartInfo.UserName = $credentials.GetNetworkCredential().UserName
> * * * * $StartInfo.Password = $credentials.GetNetworkCredential().Password
> * * * * $StartInfo.FileName = $command
> * * * * $StartInfo.Arguments = $arguments
> * * * * $StartInfo.WorkingDirectory = $workingDirectory
> * * * * $StartInfo.LoadUserProfile = $true
> * * * * $StartInfo.UseShellExecute = $false
> * * * * [System.Diagnostics.Process]::Start($StartInfo)
>
> }
>
> to test it I'm doing this:
>
> $cred = Get-Credential
>
> Impersonate [System.Management.Automation.PSCredential]$cred, "e:
> \toolshed\reflector.exe", "e:\dev\MyOperations.dll", "e:\dev"
>
> I get a runtime error "Cannot convert "System.Object[]" to
> "System.Management.Automation.PSCredential" on the line where I call
> Impersonate(). Do I have the wrong type for what get-credential
> returns?
>
> thx,
> *bob
Hi Bob,

don't use commas to separate the function arguments.

- Oisin
My System SpecsSystem Spec
Old 05-09-2008   #3 (permalink)
bobuva
Guest


 

Re: Passing credential object - what's the type?

On May 8, 5:22 pm, "Oisin (x0n) Grehan [MVP]" <ois...@xxxxxx>
wrote:
Quote:

> On May 8, 6:26 pm, bobuva <robert...@xxxxxx> wrote:
>
>
>
Quote:

> > I'm writing a script function that will take a credential object and
> > assign the username and password to members of a ProcessStartInfo
> > object. Here's the function:
>
Quote:

> > function Impersonate ([System.Management.Automation.PSCredential]
> > $credentials, [string] $command, [string] $arguments, [string]
> > $workingDirectory )
> > {
> > $StartInfo = new-object Diagnostics.ProcessStartInfo
> > $StartInfo.UserName = $credentials.GetNetworkCredential().UserName
> > $StartInfo.Password = $credentials.GetNetworkCredential().Password
> > $StartInfo.FileName = $command
> > $StartInfo.Arguments = $arguments
> > $StartInfo.WorkingDirectory = $workingDirectory
> > $StartInfo.LoadUserProfile = $true
> > $StartInfo.UseShellExecute = $false
> > [System.Diagnostics.Process]::Start($StartInfo)
>
Quote:

> > }
>
Quote:

> > to test it I'm doing this:
>
Quote:

> > $cred = Get-Credential
>
Quote:

> > Impersonate [System.Management.Automation.PSCredential]$cred, "e:
> > \toolshed\reflector.exe", "e:\dev\MyOperations.dll", "e:\dev"
>
Quote:

> > I get a runtime error "Cannot convert "System.Object[]" to
> > "System.Management.Automation.PSCredential" on the line where I call
> > Impersonate(). Do I have the wrong type for what get-credential
> > returns?
>
Quote:

> > thx,
> > bob
>
> Hi Bob,
>
> don't use commas to separate the function arguments.
>
> - Oisin
thanks. I finally got it working yesterday.
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Unable to cast COM object of type 'ADODB.RecordsetClass' to class type 'System.Object[]' Michel Posseth [MCP] .NET General 2 03-27-2008 10:04 AM
Alternate Credential passing Anatoli PowerShell 4 08-02-2007 02:14 PM
Passing a HashTable object as a parameter to a script Brillig PowerShell 8 02-07-2007 04:45 PM
Passing script function as a callback into .NET object Ilya PowerShell 0 01-20-2007 03:34 PM
Help with foreach-object and passing parameters Xanbar PowerShell 1 11-15-2006 02:55 AM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008