Correct, the profile will execute before the script called on the command line.
If you're on PS 3.0 or higher (and really should be), there is module
auto-loading, so some of the "import-module" stuff in the profile is likely
redundant.
If you want to do things based on if you're running PowerCLI, I'd try this:
if ($host.ui.rawui.WindowTitle -like '*PowerCLI*') {
PowerCLI specific stuff here
}
else {
other stuff here
}
DAMIEN SOLODOW
Senior Systems Engineer
317.447.6033 (office)
317.447.6014 (fax)
HARRISON COLLEGE
-----Original Message-----
From: [email protected] [mailto:[email protected]] On
Behalf Of Michael Leone
Sent: Wednesday, March 23, 2016 2:48 PM
To: [email protected]
Subject: [powershell] Determining what parameters were passed to Powershell.exe
as it first executes
This is a bit odd. I run PowerCLI, which is the VMware supplied snapins (and
now modules) that are specific for VMware. The new version, 6.0 Release 3, now
has modules, where previously they were only snapins. Basically, PS is started
and then executes a special
script:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noe -c ".
\"C:\Program Files (x86)\VMware\Infrastructure\vSphere
PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\" $true"
So Initialize-PowerCLIEnvironment.ps1 is what actually sets up the environment,
loads the snapins and modules, etc.
I used to use this, as my PowerShell profile:
IF (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction "SilentlyContinue") { #
Running PowerCLI
Connect-VIServer ....
}
ELSE
{
# NOT running PowerCLI
Import-Module ActiveDirectory
}
This checked to see if the VMware snapin was loaded, and - if so - connect me
to my vCenter. If VMware support was not loaded, it would import various
Microsoft-specific modules. Worked like a charm,
However, since upgrading to PowerCLI 6.0 Release 3, this doesn't work.
I tried changing the test to Get-Module, and it still failed. I had it write
out the list of loaded snapins/modules, and no VMware-related entries showed
up, as the profile executed:
write-host "Starting profile"
get-pssnapin
get-module
write-host "ending profile"
Result:
===========================
Starting profile
Name : Microsoft.PowerShell.Core
PSVersion : 4.0
Description : This Windows PowerShell snap-in contains cmdlets used to manage
components of Windows PowerShell.
Name : Microsoft.PowerShell.Utility
Path :
C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Utility\Microsoft.PowerShell.Utility.psd1
Description :
Guid : 1da87e53-152b-403e-98dc-74d7b4d63d59
Version : 3.1.0.0
ModuleBase : C:\Windows\System32\WindowsPowerShell\v1.0
ModuleType : Manifest
PrivateData :
AccessMode : ReadWrite
ExportedAliases : {}
ExportedCmdlets : {[Add-Member, Add-Member], [Add-Type, Add-Type],
[Clear-Variable, Clear-Variable], [Compare-Object, Compare-Object]...}
ExportedFunctions : {[Get-FileHash, Get-FileHash]} ExportedVariables : {}
NestedModules : {Microsoft.PowerShell.Commands.Utility.dll,
Microsoft.PowerShell.Utility}
Ending Profile
Welcome to VMware vSphere PowerCLI!
Log in to a vCenter Server or ESX host: Connect-VIServer
To find out what commands are available, type: Get-VICommand
To show searchable help for all PowerCLI commands: Get-PowerCLIHelp
Once you've connected, display all virtual machines: Get-VM If you need more
help, visit the PowerCLI community: Get-PowerCLICommunity
Copyright (C) VMware, Inc. All rights reserved.
PS P:\software\PHA Scripts>
===========================
Basically, it looks like the profile is now being executed *before* the
Initialize-PowerCLIEnvironment.ps1 (which actually loads all the VMware snapins
and modules). And so the IF test will fail, as the snapins/modules are not yet
loaded.
I thought about perhaps checking for the command line parameters that were
passed to PowerShell. For example, clicking on the PowerCLI icon actually
executes this:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noe -c ".
\"C:\Program Files (x86)\VMware\Infrastructure\vSphere
PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\" $true"
I thought that if I could enumerate the parameters passed to Powershell.exe,
then the 3rd parameter above is the C:\Program Files
(x86)\VMware\Infrastructure\vSphere
PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\.
And if so, then I am running PowerCLI, otherwise I am running stock PowerShell.
But how do I access that parameter in my profile? Do I need to actually put in
a PARAM statement in my profile? Or is that saved somewhere I can access it,
like in an environmental variable or similar?
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1