Pass C# Securestring Object to PowerShell

Answered Pass C# Securestring Object to PowerShell

  • Monday, December 10, 2012 9:27 AM
     
      Has Code

    Hi,

    I have my Password saved in a C# securestring and I want to pass it to powershell.

    Here is what I have

    powershell.AddScript("$pword = ConvertTo-SecureString " + Credential.Password);

    ...


    and this is the error

    Cannot convert argument "1", with value: "System.Security.SecureString", for "PSCredential" to type "System.Security.SecureString": "Cannot convert the "System.Security.SecureString" value of type "System.String" to type "System.Security.SecureString"
    

    Do you have any idea how to solve this? When my password were a normal string, i would use convert to securestring, but it is already an securestring Object.

    Greetings




All Replies

  • Monday, December 10, 2012 3:27 PM
     
     

    You could start here: http://technet.microsoft.com/en-us/library/hh849814.aspx

    YOur code is not PowerSHell script in htat you are trying to create a customhost in C#.  Use PowerShell command line to test your methodology.

    $SecureString = Read-Host -AsSecureString
    $StandardString = ConvertFrom-SecureString  $SecureString


    ¯\_(ツ)_/¯

  • Monday, December 10, 2012 3:54 PM
     
     

    It just thought that maybeyou were asking how to "Decrypt" a secure string.  If sio here is a method that works locally.

     [System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($securestring))

    You may have to release memeory:

    $ptr=[System.Runtime.InteropServices.marshal]::SecureStringToGlobalAllocUnicode($ss)
    [System.Runtime.InteropServices.marshal]::PtrToStringAuto($ptr)
    [System.Runtime.InteropServices.marshal]::FreeHGlobal($ptr)

    $ptr=[System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($ss)
    [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr)
    [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($ptr)

    The method you would use would depend on the way in which you are passing the SecureString; in or out of process.


    ¯\_(ツ)_/¯

  • Tuesday, December 11, 2012 8:40 AM
     
     

    It just thought that maybeyou were asking how to "Decrypt" a secure string. 

    no I have C# Code and there I have a Securestring object. Now I want to pass it to Powershell to use it for creating a Session ( Credential). I think ConvertTo or From Secure String is not the right way, becaue it is already a Securestring object ( in C#)
  • Tuesday, December 11, 2012 12:58 PM
     
     

    It just thought that maybeyou were asking how to "Decrypt" a secure string. 

    no I have C# Code and there I have a Securestring object. Now I want to pass it to Powershell to use it for creating a Session ( Credential). I think ConvertTo or From Secure String is not the right way, becaue it is already a Securestring object ( in C#)

    We don't  understand.  Just pass the object.  If you need help with this then you need to post in the C# forum for assistance with using C# and Net framework classes.

    ¯\_(ツ)_/¯

  • Tuesday, December 11, 2012 1:32 PM
     
     

    It just thought that maybeyou were asking how to "Decrypt" a secure string. 

    no I have C# Code and there I have a Securestring object. Now I want to pass it to Powershell to use it for creating a Session ( Credential). I think ConvertTo or From Secure String is not the right way, becaue it is already a Securestring object ( in C#)

    I am going to give this one more try.

    You have a SecureString object and want to pass it.  You can pass it in-process as an object.  You can pass it out-of-process as a global object.  You can convert it to a string and pass it as part of text in a file or a script.  When passed as a string it can be converted back into a SecureString object using ConvertTo-SecureString.

    This allows you to use the encrypted string in all possible ways.  ConvertTo and ConvertFrom are a pair of CmdLets.  They work together.


    ¯\_(ツ)_/¯

  • Friday, December 14, 2012 9:16 AM
     
      Has Code

    i think the problem isn't clear. what i said in the start post wasn't correct

    here is the whole code:

    PSCredential cred = new PSCredential( "bla", "blaa");

    //#--- here some converting stuff c# cred object to powershell $cred variable ---- //

    powershell.AddScript(@"$s = New-PSSession -ComputerName '" + serverName + "' -Credential $cred"); powershell.AddScript(@"$a = Invoke-Command -Session $s -ScriptBlock {" + cmdlet + "}"); powershell.AddScript(@"Remove-PSSession -Session $s"); powershell.AddScript(@"echo $a");


    maybe now it is clear what i want to do.

    my first idea was this:

    powershell.AddCommand("New-PSSession").AddParameter("ComputerName", serverName).AddParameter("Credential", cred);

    this is working. it creates a new session with the given PSCredential object from c#.

    The problem for this way is, that i have to use the session object in the rest of the code to invoke some commands. But I don't know how to rewrite the rest of the code, because I haven't the Pssesion in a variable for using.

    Any ideas?



    • Edited by Daniel R, Friday, December 14, 2012 9:16 AM
    • Edited by Daniel R, Friday, December 14, 2012 9:17 AM
    •  
  • Friday, December 14, 2012 9:52 AM
     
     Answered Has Code

    I found the answer myself:

    powershell.AddCommand("Set-Variable");
    powershell.AddParameter("Name", "cred");
    powershell.AddParameter("Value", Credential);
    
    powershell.AddScript(@"$s = New-PSSession -ComputerName '" + serverName + "' -Credential $cred");
    powershell.AddScript(@"$a = Invoke-Command -Session $s -ScriptBlock {" + cmdlet + "}");
    powershell.AddScript(@"Remove-PSSession -Session $s");
    powershell.AddScript(@"echo $a");

    Where Credential is the c# PSCredential object

    • Marked As Answer by Daniel R, Friday, December 14, 2012 9:52 AM
    •