so I have a startup script where I need to run the script as a user to collect environment variables halfway through the script I need to run a command as admin. here is the catch the execution policy is set to restricted to invoke command will not work. any suggestions?
Should be okay, then. The only problem I see is in how you’re trying to pass local variables to Invoke-Command. -PipelineVariable doesn’t work that way. Instead, try this:
I should point out that your admin username and password may as well be in plain text. They’re encrypted, but the key is right there in the script, and anyone who has that script can read the password. If you hadn’t modified it for this post with that “areallylongstring” bit, you’d have already posted your password to the whole internet.
There is no way to put a password like that into your script in such a way that it’s secure (unless you’re okay with anyone who is able to run the script also being able to retrieve the plain-text password). In PowerShell, the ideal solution is to set up a custom PSRemoting endpoint which runs as an administrator account, but the authentication / authorization to that endpoint comes from the users themselves. With that approach, you get two big advantages: no hard-coded credentials (users are authenticated as themselves), and you can limit what is done with the admin credentials by locking down the endpoint (NoLanguage mode, only one function exposed that does what you need it to do, etc.)
it does kick off robocopy how ever it is running into an old robocopy issue run as user vs run as admin. this happens when you right click on cmd prompt and select run as other user vs run as admin.
ERROR : You do not have the Backup and Restore Files user rights.
***** You need these to perform Backup copies (/B or /ZB).
ERROR : Robocopy ran out of memory, exiting.
ERROR : Invalid Parameter #%d : "%s"
ERROR : Invalid Job File, Line #%d :"%s"
Started : %hs
Source %c
Dest %c
Simple Usage :: ROBOCOPY source destination /MIR
source :: Source Directory (drive:\path or \\server\share\path).
destination :: Destination Dir (drive:\path or \\server\share\path).
/MIR :: Mirror a complete directory tree.
For more usage information run ROBOCOPY /?
**** /MIR can DELETE files as well as copy them !
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Mon Jan 18 10:21:41 2016
Source : C:\WINDOWS\CSC\v2.0.6\namespace\CAM\fldrrdr\HTN\108736\
Dest : C:\users\HTN\108736\
Files : *.*
Options : *.* /TEE /S /E /COPY:DATOU /ZB /R:0 /W:30
------------------------------------------------------------------------------
ERROR : You do not have the Manage Auditing user right.
***** You need this to copy auditing information (/COPY:U or /COPYALL).
Simple Usage :: ROBOCOPY source destination /MIR
source :: Source Directory (drive:\path or \\server\share\path).
destination :: Destination Dir (drive:\path or \\server\share\path).
/MIR :: Mirror a complete directory tree.
For more usage information run ROBOCOPY /?
**** /MIR can DELETE files as well as copy them !