function Add-Credentials { param( [parameter(mandatory=$true)][ValidateNotNullOrEmpty()][string]$Server, [parameter(mandatory=$true)][ValidateNotNullOrEmpty()][string]$Username, [parameter(mandatory=$true)][ValidateNotNullOrEmpty()][string]$Password ) $code = @" [DllImport("Advapi32.dll", SetLastError=true, EntryPoint="CredWriteW", CharSet=CharSet.Unicode)] public static extern bool CredWrite([In] ref Credential userCredential, [In] UInt32 flags); [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct Credential { public UInt32 flags; public UInt32 type; public IntPtr targetName; public IntPtr comment; public System.Runtime.InteropServices.ComTypes.FILETIME lastWritten; public UInt32 credentialBlobSize; public IntPtr credentialBlob; public UInt32 persist; public UInt32 attributeCount; public IntPtr Attributes; public IntPtr targetAlias; public IntPtr userName; } "@ Add-Type -MemberDefinition $code -Namespace "ADVAPI32" -Name 'Util' $cred = New-Object ADVAPI32.Util+Credential $cred.flags = 0 $cred.type = 2 $cred.targetName = [System.Runtime.InteropServices.Marshal]::StringToCoTaskMemUni($Server) $cred.userName = [System.Runtime.InteropServices.Marshal]::StringToCoTaskMemUni($username) $cred.attributeCount = 0 #Erhalten der Credentials: 1 = Session / 2 = Local Machine / 3 = Enterprise $cred.persist = 3 $cred.credentialBlobSize = [System.Text.Encoding]::Unicode.GetBytes($Password).length $cred.credentialBlob = [System.Runtime.InteropServices.Marshal]::StringToCoTaskMemUni($Password) $result = [ADVAPI32.Util]::CredWrite([ref]$cred,0) return $result } Add-Credentials -Server '172.24.11.14' -Username 'digitaldata\KammM' -Password 'LmaA'