Remoting

TBD

Remoting and P/Invoke

Using a Win32 API call on a remote computer is a bit tricky. One approach is to create the type remotely, and then use it. Example:

Invoke-Command -ComputerName gaudos -Credential $Credential -ScriptBlock {
    $body = @"
[DllImport("shell32.dll")]
private static extern int SHGetKnownFolderPath(
        [MarshalAs(UnmanagedType.LPStruct)] 
        Guid rfid,
        uint dwFlags,
        IntPtr hToken,
        out IntPtr pszPath
);

public static string GetKnownFolderPath(Guid rfid)
{
    IntPtr pszPath;
    if (SHGetKnownFolderPath(rfid, 0, IntPtr.Zero, out pszPath) != 0)
        return ""; // add whatever error handling you fancy
    string path = Marshal.PtrToStringUni(pszPath);
    Marshal.FreeCoTaskMem(pszPath);
    return path;
}
"@ 

    $caller = Add-Type -Name "shell32" -MemberDefinition $body -Namespace Win32Functions -PassThru

    $caller::GetKnownFolderPath("33E28130-4E1E-4676-835A-98395C3BC3BB")
}

A more primitive alternative that may be convenient for more complicated scripts is to copy the file directly to the target machine, and then invoke the script remotely. (Instead of copy, you can also put in a shared folder --- TODO I believe there are certain limitations with that.)

Resources

Some related resources:

results matching ""

    No results matching ""