Share via


PS-Script Import-Clixml Problem with password

Question

Wednesday, March 18, 2020 4:42 PM

Hi guys

Im scheduling a ps-script for transfer of data between directories. I need to provide local credentials from destination server and I am doing with import-clixml.

It doesnt work out. If I am running the script I get this error:

Import-Clixml : Data at the root level is invalid. Line 1, position 1.
At C:\scripts\xxx.ps1:13 char:36
+ $password = ConvertTo-SecureString(Import-Clixml -Path  C:\Temp\pw_file.xml ...
+                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Clixml], XmlException
    + FullyQualifiedErrorId : System.Xml.XmlException,Microsoft.PowerShell.Commands.ImportClixmlCommand
 
New-Object : Exception calling ".ctor" with "2" argument(s): "Cannot process argument because the value of argument "password" is null. Change the 
value of argument "password" to a non-null value."
At C:\scripts\xxx.ps1:15 char:16
+ $credentials = New-Object System.Management.Automation.PSCredential($user,$p ...
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

I use this config:

$password = ConvertTo-SecureString(Import-Clixml -Path C:\Temp\pw_file.xml)
$user= 'testuser'
$credentials = New-Object System.Management.Automation.PSCredential($benutzer,$password)

#drive mapping

New-PSDrive –Name S –PSProvider FileSystem –Root \\testserver\inbox -Credential $credentials
New-PSDrive –Name T –PSProvider FileSystem –Root \\testserver\outbox -Credential $credentials
...

The pw_file.xml contains the password from the user in plain text (I know not the best way...). 
Why do I get this error?

Greets

All replies (5)

Monday, March 23, 2020 10:19 AM ✅Answered

However, I've now done it with net use command.


Wednesday, March 18, 2020 5:15 PM

You cannot convert a file into a secure string for a password.  

You can only use a plain text string to create a secure password.

\(ツ)_/


Thursday, March 19, 2020 8:36 AM

Alright, I'm not really used to PS. I've tried it the following way:

$password ='plainpassword'
$user= 'testuser'
$credentials = New-Object System.Management.Automation.PSCredential($user,$password)
New-Object : Cannot find an overload for "PSCredential" and the argument count: "2".
At line:7 char:16
+ $credentials = New-Object System.Management.Automation.PSCredential($user,$p ...
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
 

What I'm doing wrong? The credential manager still pops up...

Can show me an explain how it could look like please?


Thursday, March 19, 2020 8:53 AM

$user= 'testuser'
$password ='plainpassword'

$secpwd = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = [System.Management.Automation.PSCredential]::new($user,$secpwd)

\(ツ)_/


Monday, March 23, 2020 9:27 AM

I've tested this, but I got this error:

Method invocation failed because [System.Management.Automation.PSCredential] does not contain a method named 'new'.

I did little research and this error may appear if am using an older version. They considered Automation was the answer but in my case both does not work as I already tested above

System.Management.Automation.PSCredential