Search This Blog

Thursday, March 07, 2013

Powershell scripts and .Net assemblies–force PS to run later .Net CLR

Recently i was writing a PS script to check a list of websites. To do this I was using a .net class

HttpWebRequest


So I wrote the script in powerGUI on the server where I wanted the script to run. I got it working within powerGUI, so I thought I would then schedule the script.



But when I run the script from the tesk scheduler I was met with an error, stating that one of the objects I wanted to use didnot contain a method.




Method invocation failed because [System.Net.HttpWebResponse] doesn't contain a method named 'Dispose'.




What?!, it worked fine in the powerGUI on the same server.



Anyway I eventually tracked this down to the fact that powershell 2.0 starts with an older .Net framework CLR. So I needed to get powershell to run a later version.



I found an article here, it explains the problem and the solutions nicely.



http://viziblr.com/news/2012/5/16/the-easy-way-to-run-powershell-20-using-net-framework-40.html



I created the xml files in the locations specified and it solved my problems.



C:\Windows\System32\WindowsPowerShell\v1.0




  • powershell.exe.config


  • powershell_ise.exe.config



XML contents




<?xml version="1.0"?>

<configuration>


    <startup useLegacyV2RuntimeActivationPolicy="true">


        <supportedRuntime version="v4.0.30319"/>


        <supportedRuntime version="v2.0.50727"/>


    </startup>


</configuration>


Share/Bookmark

Monday, March 04, 2013

Enable Group Policy logging

Note: A great tool for viewing the GPO logs created in this article is available for free here. http://www.sysprosoft.com/policyreporter.shtml

Its great tool.

Group Policy logging can be enabled by the addition (or changing) a registry entry.

Windows 2000/2003

Registry


[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]

"UserEnvDebugLevel"=dword:00030002

Valid entries

  • NONE 0x00000000
  • NORMAL 0x00000001
  • VERBOSE 0x00000002
  • LOGFILE 0x00010000
  • DEBUGGER 0x00020000

These values can be combined.

0x00030002

i.e. for logfile, debugger and verboose

%windir%\debug\usermode\UserEnv.log

Once setup a reboot may be needed as it doesnt seem to create the folder usermode until a reboot has been completed. However I have seen a posting that said that you can manually create the usermode folder and that the UserEnv.log file will be created without a reboot, when the gpupdate is called.

 

Windows 2008

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Diagnostics]

"GPSvcDebugLevel"=dword:00030002

Valid entries

  • NONE 0x00000000
  • NORMAL 0x00000001
  • VERBOSE 0x00000002
  • LOGFILE 0x00010000
  • DEBUGGER 0x00020000

These values can be combined.

0x00030002

i.e. for logfile, debugger and verbose

%WINDIR%\debug\usermode\gpsvc.log

Once setup a reboot may be needed as it doesnt seem to create the folder usermode until a reboot has been completed. However I have seen a posting that said that you can manually create the usermode folder and that the UserEnv.log file will be created without a reboot, when the gpupdate is called.

References

http://technet.microsoft.com/en-us/library/cc759167(v=ws.10).aspx

http://www.sysprosoft.com/policyreporter.shtml

http://technet.microsoft.com/en-us/magazine/dd315424.aspx


Share/Bookmark