Run as different user Script
Hello.
When new hire account has to be created, The first line level copy paste the "content" of an email sent to us (the way a program send us the information...).
And First line only has double clik on the bat file to execute the creation. Those agents used to run the script on the Hubcas server. But I have been asked to make them run the script from their local machine in Windows7.
I did install EMC and shell console, If I do Shift and tight click I can manage to exeute console as different user (administrator user) and make any change. But I would like to avoid the right click run as to open the consoles AND to run the below script
as administrator.
I search on the Net but I failed when trying to run as the script or console. Can you please help me??
BAT:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'C:\Scripts\ACCOUNT_CREATION\EXCHANGE.ps1' "
PS1:
import-csv "C:\Scripts\EXCHANGE.csv" | foreach {enable-MailBox -identity $_.identity -PrimarySmtpAddress $_.PrimarySmtpAddress -Database $_.Database} | Set-Mailbox -emailaddresspolicyenabled:$True
Start-EdgeSynchronization
CONTENT:
Identity,PrimarySmtpAddress,database
company/Uk/Users/B1/SMITH JOHN,"JOHN.smith@company.com","servcex04\SG11\Uk"
Many Thanks,
Graig
March 6th, 2013 1:35pm
Hi Greg,
As you are trying to connect to Exchange remotely, you need to enter a user name and password. If you are not ready for prompting for this each time, you may store those in a text file and can be accessed through the script. So you need to modify your script
little bit (add some additional lines of scripting)
First, we need to get our password, then pump it into a file. Doing this encodes the password and stores it in our output file so no-one can read it.
See a useful thread here:
http://blogs.technet.com/b/robcost/archive/2008/05/01/powershell-tip-storing-and-using-password-credentials.aspxRegards from ExchangeOnline
Windows Administrator's Area
Free Windows Admin Tool Kit Click here and download it now
March 6th, 2013 2:00pm
Sorry I just don't wanna do something wrong..
So I run from my Hubcas that command:
read-host -assecurestring | convertfrom-securestring | out-file C:\cred.txt
Then I add in my ps1 file the two fisrt line? I am not an expert and have no testing environnement :-S
$password = get-content C:\cred.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "myusername",$pass
import-csv "C:\Scripts\EXCHANGE.csv" | foreach {enable-MailBox -identity $_.identity -PrimarySmtpAddress $_.PrimarySmtpAddress -Database $_.Database} | Set-Mailbox -emailaddresspolicyenabled:$True
Thanks, Greg
March 6th, 2013 4:26pm
Greg,
Add the code as the first lines of your PS1Regards from ExchangeOnline
Windows Administrator's Area
Free Windows Admin Tool Kit Click here and download it now
March 6th, 2013 5:01pm
Thanks you. So It should read as:
read-host -assecurestring | convertfrom-securestring | out-file C:\cred.txt
$password = get-content C:\cred.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "myusername",$pass
import-csv "C:\Scripts\EXCHANGE.csv" | foreach {enable-MailBox -identity $_.identity -PrimarySmtpAddress $_.PrimarySmtpAddress -Database $_.Database} | Set-Mailbox -emailaddresspolicyenabled:$True
As I do not have a testing environnement. Can you please tell me if I'll be prompt to enter my credentials?
Anyway, thank you very much
Greg
March 6th, 2013 5:17pm
Hi,
There are few corrections. Substitute your username instead of "myusername" and $password instead of $pass
While starting the script, it will prompt in a blank line to enter the password and press enter again to continue the script.
Regards from ExchangeOnline
Windows Administrator's Area
Free Windows Admin Tool Kit Click here and download it now
March 6th, 2013 5:57pm
Hello,
I then ran the below ps1 file:
read-host -assecurestring | convertfrom-securestring | out-file C:\cred.txt
$password = get-content C:\cred.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "admin-greg",$password
import-csv "C:\Scripts\ServiceDesk\AUTO-UPDATE\EXCHANGE-ACCOUNT_CREATION\EXCHANGE.csv" | foreach {enable-MailBox -identity $_.identity -PrimarySmtpAddress $_.PrimarySmtpAddress -Database $_.Database} | Set-Mailbox -emailaddresspolicyenabled:$True
Start-EdgeSynchronization -server HUBCAS01.company.intra
And got the below error messages:
[PS] C:\Scripts\ACCOUNT_CREATION>.\EXCHANGE.ps1
ConvertFrom-SecureString : Cannot process argument because the value of argumen
t "SecureString" is invalid. Change the value of the "SecureString" argument an
d run the operation again.
At C:\Scripts\ACCOUNT_CREATION\EXCHANGE.ps1:1 char:53
+ read-host -assecurestring | convertfrom-securestring <<<< | out-file C:\cred
.txt
+ CategoryInfo : InvalidArgument: (:) [ConvertFrom-SecureString],
PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ConvertFr
omSecureStringCommand
Get-Content : Cannot find path 'C:\cred.txt' because it does not exist.
At C:\Scripts\ACCOUNT_CREATION\EXCHANGE.ps1:2 char:24
+ $password = get-content <<<< C:\cred.txt | convertto-securestring
+ CategoryInfo : ObjectNotFound: (C:\cred.txt:String) [Get-Conten
t], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCo
ntentCommand
New-Object : Exception calling ".ctor" with "2" argument(s): "Cannot process ar
gument because the value of argument "password" is null. Change the value of ar
gument "password" to a non-null value."
At C:\Scripts\ACCOUNT_CREATION\EXCHANGE.ps1:3 char:26
+ $credentials = new-object <<<< -typename System.Management.Automation.PSCred
ential -argumentlist "admin-greg",$password
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvoca
tionException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
Shell.Commands.NewObjectCommand
Database "tlgcex01\SG03\innetis-otia" was not found. Please make sure you have
typed it correctly.
At C:\Scripts\ACCOUNT_CREATION\EXCHANGE.ps1:5 char:99
+ import-csv "C:\Scripts\ServiceDesk\AUTO-UPDATE\EXCHANGE-ACCOUNT_CREATION\EXCH
ANGE.csv" | foreach { <<<< enable-MailBox -identity $_.identity -PrimarySmtpAdd
ress $_.PrimarySmtpAddress -Database $_.Database} | Set-Mailbox -emailaddresspo
licyenabled:$True
+ CategoryInfo : NotSpecified: (:) [], ManagementObjectNotFoundEx
ception
+ FullyQualifiedErrorId : 270AB2F0
Enable-Mailbox : Cannot bind parameter 'Identity'. Cannot convert value "" to t
ype "Microsoft.Exchange.Configuration.Tasks.UserIdParameter". Error: "The param
eter value of this type Microsoft.Exchange.Configuration.Tasks.UserIdParameter
cannot be empty.
Parameter name: identity"
At C:\Scripts\ACCOUNT_CREATION\EXCHANGE.ps1:5 char:123
+ import-csv "C:\Scripts\ServiceDesk\AUTO-UPDATE\EXCHANGE-ACCOUNT_CREATION\EXCH
ANGE.csv" | foreach {enable-MailBox -identity <<<< $_.identity -PrimarySmtpAdd
ress $_.PrimarySmtpAddress -Database $_.Database} | Set-Mailbox -emailaddresspo
licyenabled:$True
+ CategoryInfo : InvalidArgument: (:) [Enable-Mailbox], Parameter
BindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Exchang
e.Management.RecipientTasks.EnableMailbox
Start-EdgeSynchronization : Access is denied
At C:\Scripts\ACCOUNT_CREATION\EXCHANGE.ps1:7 char:26
+ Start-EdgeSynchronization <<<< -server HUBCAS01.company.intra
+ CategoryInfo : NotSpecified: (:) [Start-EdgeSynchronization], W
in32Exception
+ FullyQualifiedErrorId : 76889C93,Microsoft.Exchange.Management.EdgeSync.
SyncNowTask
[PS] C:\Scripts\ACCOUNT_CREATION>
Can you please tell me what step have I missed?
Thanks for you help.
Greg
March 7th, 2013 11:07am
Hi Greg,
You need some modifications.
First is: Save the below lines as one.ps1
read-host -assecurestring | convertfrom-securestring | out-file C:\cred.txt
$password = get-content C:\cred.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "admin-greg",$password
Second is: Save the old script as two.ps1
Run one.ps1 first. Type password and press enter. This will create a text cred.txt at c:\ drive. Then run two.ps1
Regards from ExchangeOnline
Windows Administrator's Area
Free Windows Admin Tool Kit Click here and download it now
March 7th, 2013 3:03pm
Thank you!
But when I run the one.ps1 an got that error:
[PS] C:\Scripts\ACCOUNT_CREATION>.\one.ps1
ConvertFrom-SecureString : Cannot process argument because the value of argumen
t "SecureString" is invalid. Change the value of the "SecureString" argument an
d run the operation again.
At C:\Scripts\ACCOUNT_CREATION\one.ps1:1 char:53
+ read-host -assecurestring | convertfrom-securestring <<<< | out-file C:\cred
.txt
+ CategoryInfo : InvalidArgument: (:) [ConvertFrom-SecureString],
PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ConvertFr
omSecureStringCommand
Get-Content : Cannot find path 'C:\cred.txt' because it does not exist.
At C:\Scripts\ACCOUNT_CREATION\one.ps1:2 char:24
+ $password = get-content <<<< C:\cred.txt | convertto-securestring
+ CategoryInfo : ObjectNotFound: (C:\cred.txt:String) [Get-Conten
t], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCo
ntentCommand
New-Object : Exception calling ".ctor" with "2" argument(s): "Cannot process ar
gument because the value of argument "password" is null. Change the value of ar
gument "password" to a non-null value."
At C:\Scripts\ACCOUNT_CREATION\one.ps1:3 char:26
+ $credentials = new-object <<<< -typename System.Management.Automation.PSCred
ential -argumentlist "admin-greg",$password
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvoca
tionException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
Shell.Commands.NewObjectCommand
It dos not create the file cred.txt
March 7th, 2013 6:17pm
Hello,
Anyone else could help me on that?
My TSA is logged on the domain with an basic user profil and the OS is W7.
The TSA needs to open a bat file (Which open a shell script to handle administrative tasks such as user deactivatiojn, creation etc..). He need to open it as admin-user.
Many thanks in advance,
Graig
Free Windows Admin Tool Kit Click here and download it now
March 13th, 2013 10:02am