locked
I can't seem to use System management Automation no matter what i try. RRS feed

  • Question

  • So i made this script to open a script as a sertain user as an admin.

    Add-Type -AssemblyName System.management.automation  (or #add-type -path 'C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll')

    $username = "IAmLegeon"
    $password = get-content 'N:\*\*.txt' | ConvertTo-SecureString
    $cred = New-Object -typename system.managment.automation.PScredential -argumentlist $username, $password

    Start-Process powershell.exe -Credential $cred -hidden -ArgumentList "Start-Process powershell.exe -Verb runAs"

    Remove-Item C:\Windows\System32\spool\PRINTERS\* -recurse -force

    I get the error 
    New-Object : Cannot find type [system.managment.automation.PScredential]: verify that the assembly containing this type is loaded.

    Also some other errors but those don't realy matter yet.

    Wednesday, December 20, 2017 3:14 PM

All replies

  • You have a typo in your script.  Also, try this way:

    $cred = New-Object System.Management.Automation.PSCredential($username,$password)


    • Edited by BF22 Wednesday, December 20, 2017 6:12 PM
    Wednesday, December 20, 2017 6:11 PM
  • THx it seems to work now. Except it is telling me my username or password are incorrect even tho i'm 90% sure that they are. Could it be a mistake in converting the string?


    • Edited by NTanghe2000 Thursday, December 21, 2017 3:51 PM
    Thursday, December 21, 2017 3:44 PM
  • Hi,

    Based on my research, please have a try with the following script to create a credential and see if the issue still remains. For your reference, hope it is helpful to you:
    $username = 'username'
    $password = ConvertTo-SecureString -String 'password' -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential($username,$password)
    

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, December 22, 2017 9:23 AM
  • Hi,

    Just checking in to see if the information provided was helpful. Does the script work?

    Please let us know if you would like further assistance.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, December 26, 2017 1:38 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, December 28, 2017 9:56 AM
  • cross verify the get-content 'N:\*\*.txt' output,this logic will go for a toss if you have multiple .txt files in the same directory. Check for any leading or trailing spaces in the desired text file.

    Regards kvprasoon

    Thursday, December 28, 2017 1:30 PM