mystikal1000
asked on
Powershell script that will read all of the Outlook profiles and run a command on each Mail profile.
I need a powershell script that will do the following.
1. I have a powershell command that will read the subkeys of a mail profile
The output of the subkeys are below...
Name Property
---- --------
cool Outlook Profile
Outlook Profile
outlook
2. I need each of the subkeys into a variable, how do I do that in Powershell?
3. After I have that answer, I would like to run the command for each subkey variable, inst.exe /u /service dmol /p "Variable=mailprofile"
I really appreciate the feedback!
1. I have a powershell command that will read the subkeys of a mail profile
Get-ChildItem -Path HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles
The output of the subkeys are below...
Name Property
---- --------
cool Outlook Profile
Outlook Profile
outlook
2. I need each of the subkeys into a variable, how do I do that in Powershell?
3. After I have that answer, I would like to run the command for each subkey variable, inst.exe /u /service dmol /p "Variable=mailprofile"
I really appreciate the feedback!
ASKER
Thanks Jeremy, it looks good when it writes the host command, but fails when I invoke it. See error below.
I accidentally left out the "program files (x86)" path, also I need one for 32-bit, which is just "program files".
Error:
I accidentally left out the "program files (x86)" path, also I need one for 32-bit, which is just "program files".
#Get the profile names from Registry Key
$ProfileNames = Get-ChildItem -Path HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles | %{Split-Path $_.Name -Leaf}
#loop through profile names
foreach ($profilename in $ProfileNames) {
$command = "C:\Program Files (x86)\Ops hxrext\op extensions\inst.exe /u /service dmol /p `"$($profilename)`""
#Write-Host $command
Invoke-Expression $command
}
Error:
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:19
+ C:\Program Files (x86)\Ops hxrext\op extensions\inst. ...
+ ~~~
+ CategoryInfo : ObjectNotFound: (x86:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:19
+ C:\Program Files (x86)\Ops hxrext\op extensions\inst. ...
+ ~~~
+ CategoryInfo : ObjectNotFound: (x86:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:19
+ C:\Program Files (x86)\Ops hxrext\op extensions\inst. ...
+ ~~~
+ CategoryInfo : ObjectNotFound: (x86:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
If the path has spaces, it needs to be surrounded by quotes. e.g.:
#Get the profile names from Registry Key
$ProfileNames = Get-ChildItem -Path HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles | %{Split-Path $_.Name -Leaf}
#loop through profile names
foreach ($profilename in $ProfileNames) {
$command = "`"C:\Program Files (x86)\Ops hxrext\op extensions\inst.exe`" /u /service dmol /p `"$($profilename)`""
Write-Host $command
#Invoke-Expression $command
}
Note the use of the backtick (`) in the script. It forces Powershell to interpret the double quote literally instead of using it as a string delimiter. For more info see this link: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6
ASKER
Updated it, now it gives me this error.
Invoke-Expression : At line:1 char:88
+ ... instl.exe" /u /service dmol /p "cool"
+ ~
You must provide a value expression on the right-hand side of the '/' operator.
At line:1 char:88
+ ... instl.exe" /u /service dmol /p "cool"
+ ~
Unexpected token 'u' in expression or statement.
At line:9 char:5
+ Invoke-Expression $command
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : ExpectedValueExpression,Microsoft.Po werShell.C ommands.In vokeExpres sionComman d
Invoke-Expression : At line:1 char:88
+ ... instl.exe" /u /service dmol /p "Default Outlook Profile"
+ ~
You must provide a value expression on the right-hand side of the '/' operator.
At line:1 char:88
+ ... instl.exe" /u /service dmol /p "Default Outlook Profile"
+ ~
Unexpected token 'u' in expression or statement.
At line:9 char:5
+ Invoke-Expression $command
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : ExpectedValueExpression,Microsoft.Po werShell.C ommands.In vokeExpres sionComman d
Invoke-Expression : At line:1 char:88
+ ... instl.exe" /u /service dmol /p "outlook"
+ ~
You must provide a value expression on the right-hand side of the '/' operator.
At line:1 char:88
+ ... instl.exe" /u /service dmol /p "outlook"
+ ~
Unexpected token 'u' in expression or statement.
At line:9 char:5
+ Invoke-Expression $command
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : ExpectedValueExpression,Microsoft.Po werShell.C ommands.In vokeExpres sionComman d
ASKER
When I run it from the command line it works fine...this is the correct syntax
C:\Program Files (x86)\Ops hxrext\op extensions\inst.exe /u /service dmol /p "Outlook"
- Where "Outlook" is the name of the e-mail profile.
C:\Program Files (x86)\Ops hxrext\op extensions\inst.exe /u /service dmol /p "Outlook"
- Where "Outlook" is the name of the e-mail profile.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Crazy, Powershell ISE x86 crashes when I run that command.
Can you run the 64bit version?
ASKER
Same issue.
Strange indeed. So I'm not sure why it's crashing. What is this inst.exe program?
You could try something like this so that it will just crash a new process:
You could try something like this so that it will just crash a new process:
#Get the profile names from Registry Key
$ProfileNames = Get-ChildItem -Path HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles | %{Split-Path $_.Name -Leaf}
#loop through profile names
foreach ($profilename in $ProfileNames) {
$ExePath = "C:\Program Files (x86)\Ops hxrext\op extensions\inst.exe"
$ExeArgs = "/u /service dmol /p `"$($profilename)`""
Start-Process -FilePath $ExePath -ArgumentList $ExeArgs
}
Change $command back to this:
$command = "`"C:\Program Files (x86)\Ops hxrext\op extensions\inst.exe`" /u /service dmol /p `"$($profilename)`""And instead of using Invoke-Expression, use
cmd /c $command
ASKER
Michael, I receive this error message
cmd : 'C:\Program' is not recognized as an internal or external command,
At line:9 char:5
+ cmd /c $command
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: ('C:\Program' is...ternal command,:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
operable program or batch file.
cmd : 'C:\Program' is not recognized as an internal or external command,
At line:9 char:5
+ cmd /c $command
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: ('C:\Program' is...ternal command,:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
operable program or batch file.
ASKER
Thanks Jeremy, I was able to figure the crash. It had to do with our antimalware software. I appreciate your help!
Glad to help. :)
Open in new window
You'll notice that I have Write-Host $command and #Invoke-Expression $command in the script. Write-Host will just echo out what command it will run. If that looks correct when you run the script. You can then remove the hash (#) from in front of Invoke-Expression (i.e. Invoke-Expression $command) and it will execute the command.
Let me know if that works for you.