Add-PSSnapin Issue in PowerShell 2.0
-
יום רביעי 14 מרץ 2012 06:30I have just touched PS for less than a month and want to consult with you here a issue I came across currently.
My bat script firstly calls a PS script "ps-1" to do some thing. "ps-1" will add a snap-in "A" firstly and then calls the PS script "ps-1-1", "ps-1-2" and "ps-1-3" subsequently. Each child script "ps-1-i"(i=1,2,3)also uses that snap-in “A”. In the current implementation, the child script will execute the "Add-PSSnapin" command in its own firstly in order to use it. Therefore, a lot of time is wasted on the "Add-PSSnapin“ action... So is there a way that the snap-in "A" is added once and all the subsequent child PS scripts can share the space?
I am running the code on the Win2008-R2 OS with the Powshell V2.0 installed by default.
Thank you in advance!
כל התגובות
-
יום רביעי 14 מרץ 2012 06:36Ideally, it should be clubbed on a single script with different functions, which will get this fixed.
Shaba
-
יום רביעי 14 מרץ 2012 07:04
In fact, I have searched for some solutions and one is that calling the script "ps-1" with format "PowerShell.exe -sta -File $scriptPath $paraList". However, that seems not to take into effect.... Again, I added the code section
in to the parent script "ps-1.ps1" but still invalid.$host.Runspace.ThreadOptions = "ReuseThread"
-
יום חמישי 15 מרץ 2012 06:37מנחה דיון
Hi,
I agree with Shaba, we'd better change these script into functions. In this way, we could only add snapin once at the begin of the script.
Please also check the below links for information:
http://technet.microsoft.com/en-us/library/bb963745.aspx
Best Regards,
Yan Li
Yan Li
TechNet Community Support
- סומן כתשובה על-ידי Yan Li_Microsoft Contingent Staff, Moderator יום שני 19 מרץ 2012 01:59
-
יום חמישי 15 מרץ 2012 11:18
Alternatively, you can modify your scripts to check to see if the snapin is loaded first:
if (-not ((get-pssnapin) -match <your snapin name>)){ load-pssnapin <your snapin name>}[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
- סומן כתשובה על-ידי Yan Li_Microsoft Contingent Staff, Moderator יום שני 19 מרץ 2012 02:00
-
יום חמישי 15 מרץ 2012 12:11
If you are running from a .bat file I assume you are calling powershell.exe. Specifying a profile to load could give you a way to ensure the designated SnapIn's are loaded in combination with mjolinor's test.- סומן כתשובה על-ידי Yan Li_Microsoft Contingent Staff, Moderator יום שני 19 מרץ 2012 02:00