Monday 3 August 2009

Get-System.Environment.ps1

  1. <#
  2. .SYNOPSIS
  3. This script demonstrates the use of System.Environment
  4. .DESCRIPTION
  5. This script Uses a variety of the members of System.Environment and is
  6. a re-write of a MSDN Sample Script
  7. .NOTES
  8. File Name : Get-System.Environment
  9. Author : Thomas Lee - tfl@psp.co.uk
  10. Requires : PowerShell V2 CTP3
  11. .LINK
  12. This script posted to:
  13. http://www.pshscripts.blogspot.com
  14. MSDN Sample posted at:
  15. http://msdn.microsoft.com/en-us/library/system.environment.aspx
  16. .EXAMPLE
  17. PSH [C:\foo]: .\Get-System.Environment.ps1' foo1 foo2 foo3
  18. My system drive is C: and my system root is C:\Windows
  19. -- Environment members --
  20. CommandLine : "C:\Users\tfl\Desktop\psp\PowerShellPlus.exe"
  21. GetCommandLineArgs: C:\Users\tfl\Desktop\psp\PowerShellPlus.exe
  22. CurrentDirectory : C:\Users\tfl\Desktop\psp
  23. ExitCode : 0
  24. HasShutdownStarted: False
  25. MachineName : COOKHAM8
  26. NewLine :
  27. first line
  28. second line
  29. third line
  30. OSVersion : Microsoft Windows NT 6.0.6001 Service Pack 1
  31. StackTrace : ' at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
  32. at System.Environment.get_StackTrace()
  33. at getter(Object )
  34. at System.Management.Automation.DotNetAdapter.PropertyGet(PSProperty property)
  35. at System.Management.Automation.Adapter.BasePropertyGet(PSProperty property)
  36. at System.Management.Automation.PSProperty.GetAdaptedValue()
  37. at System.Management.Automation.PSProperty.get_Value()
  38. at System.Management.Automation.PropertyReferenceNode.GetValue(PSObject obj, Object property, ExecutionContext context)
  39. at System.Management.Automation.PropertyReferenceNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
  40. at System.Management.Automation.ExpressionNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
  41. at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
  42. at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
  43. at System.Management.Automation.StatementListNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
  44. at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
  45. at System.Management.Automation.ScriptCommandProcessor.ExecuteWithCatch(ParseTreeNode ptn, Array inputToProcess)
  46. at System.Management.Automation.ScriptCommandProcessor.RunClause(ParseTreeNode clause, Object dollarUnderbar, Object inputToProcess)
  47. at System.Management.Automation.ScriptCommandProcessor.Complete()
  48. at System.Management.Automation.CommandProcessorBase.DoComplete()
  49. at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)
  50. at System.Management.Automation.PipelineNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
  51. at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
  52. at System.Management.Automation.StatementListNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
  53. at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
  54. at System.Management.Automation.ScriptCommandProcessor.ExecuteWithCatch(ParseTreeNode ptn, Array inputToProcess)
  55. at System.Management.Automation.ScriptCommandProcessor.RunClause(ParseTreeNode clause, Object dollarUnderbar, Object inputToProcess)
  56. at System.Management.Automation.ScriptCommandProcessor.Complete()
  57. at System.Management.Automation.CommandProcessorBase.DoComplete()
  58. at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)
  59. at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecute(Array input, Hashtable errorResults)
  60. at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
  61. at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()
  62. at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  63. at System.Threading.ExecutionContext.runTryCode(Object userData)
  64. at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
  65. at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
  66. at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  67. at System.Threading.ThreadHelper.ThreadStart()'
  68. SystemDirectory : C:\Windows\system32
  69. TickCount : 98385431
  70. UserDomainName : COOKHAM
  71. UserInteractive : True
  72. UserName : tfl
  73. Version : 2.0.50727.3074
  74. WorkingSet : 49385472
  75. ExpandEnvironmentVariables:
  76. My system drive is C: and my system root is C:\Windows
  77. GetEnvironmentVariable:
  78. My temporary directory is C:\Users\tfl\AppData\Local\Temp.
  79. GetEnvironmentVariables:
  80. Name Value
  81. ---- -----
  82. Path C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\b...
  83. TEMP C:\Users\tfl\AppData\Local\Temp
  84. SESSIONNAME Console
  85. PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PSC1
  86. USERDOMAIN COOKHAM
  87. PROCESSOR_ARCHITECTURE x86
  88. ProgramW6432 C:\Program Files
  89. APPDATA C:\Users\tfl\AppData\Roaming
  90. windir C:\Windows
  91. LOCALAPPDATA C:\Users\tfl\AppData\Local
  92. CommonProgramW6432 C:\Program Files\Common Files
  93. TMP C:\Users\tfl\AppData\Local\Temp
  94. USERDNSDOMAIN COOKHAM.NET
  95. USERPROFILE C:\Users\tfl
  96. ProgramFiles C:\Program Files (x86)
  97. CommonProgramFiles(x86) C:\Program Files (x86)\Common Files
  98. FP_NO_HOST_CHECK NO
  99. HOMEPATH \Users\tfl
  100. COMPUTERNAME COOKHAM8
  101. CLASSPATH .;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip
  102. PROCESSOR_ARCHITEW6432 AMD64
  103. ProgramData C:\ProgramData
  104. NUMBER_OF_PROCESSORS 8
  105. PROCESSOR_IDENTIFIER Intel64 Family 6 Model 23 Stepping 6, GenuineIntel
  106. SystemDrive C:
  107. SystemRoot C:\Windows
  108. ComSpec C:\Windows\system32\cmd.exe
  109. LOGONSERVER \\COOKHAM1
  110. ProgramFiles(x86) C:\Program Files (x86)
  111. PSMODULEPATH C:\Users\tfl\Documents\WindowsPowerShell\Modules;C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules
  112. CommonProgramFiles C:\Program Files (x86)\Common Files
  113. PROCESSOR_LEVEL 6
  114. PROCESSOR_REVISION 1706
  115. QTJAVA C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip
  116. ALLUSERSPROFILE C:\ProgramData
  117. VS90COMNTOOLS C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\
  118. PUBLIC C:\Users\Public
  119. USERNAME tfl
  120. OS Windows_NT
  121. HOMEDRIVE C:
  122. GetFolderPath: C:\Windows\system32
  123. GetLogicalDrives: C:\, D:\, E:\, F:\, G:\, H:\, I:\, J:\, K:\, L:\, Q:\, X:\
  124. .PARAMETER
  125. None, but if any are provided, they are displayed above
  126. #>
  127. ##
  128. # Start of Script
  129. ##
  130. # Setup two variables for later use
  131. [String] $str;
  132. [string] $nl = [System.Environment]::NewLine
  133. # Now display members of the environment class
  134. "-- Environment members --"
  135. # Assume: You Invoke this sample with an arbitrary set of command line arguments.
  136. "CommandLine : {0}" -f [system.environment]::CommandLine
  137. $arguments = [System.Environment]::GetCommandLineArgs()
  138. "GetCommandLineArgs: {0}" -f [system.string]::join(", ", $arguments)
  139. # <-- Keep this information secure! -->
  140. "CurrentDirectory : {0}" -f [System.Environment]::CurrentDirectory
  141. "ExitCode : {0}" -f [System.Environment]::ExitCode
  142. "HasShutdownStarted: {0}" -f [System.Environment]::HasShutdownStarted
  143. # <-- Keep this information secure! -->
  144. "MachineName : {0}" -f [System.Environment]::MachineName
  145. "NewLine : {0} first line{0} second line{0} third line" -f [System.Environment]::NewLine
  146. "OSVersion : {0}" -f [System.Environment]::OSVersion.ToString()
  147. "StackTrace : '{0}'" -f [System.Environment]::StackTrace
  148. # <-- Keep this information secure! -->
  149. "SystemDirectory : {0}" -f [System.Environment]::SystemDirectory
  150. "TickCount : {0}" -f [System.Environment]::TickCount
  151. # <-- Keep this information secure! -->
  152. "UserDomainName : {0}" -f [System.Environment]::UserDomainName
  153. "UserInteractive : {0}" -f [System.Environment]::UserInteractive
  154. # <-- Keep this information secure! -->
  155. "UserName : {0}" -f [System.Environment]::UserName
  156. "Version : {0}" -f [System.Environment]::Version.ToString()
  157. "WorkingSet : {0}" -f [System.Environment]::WorkingSet
  158. # No example for exit(exitCode) because doing so would terminate this example.
  159. # <-- Keep this information secure! -->
  160. $query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
  161. $str = [System.Environment]::ExpandEnvironmentVariables($query)
  162. "ExpandEnvironmentVariables: {0} {1}" -f $nl,$str
  163. "GetEnvironmentVariable: {0} My temporary directory is {1}." -f $nl,[System.Environment]::GetEnvironmentVariable("TEMP")
  164. ""
  165. "GetEnvironmentVariables: "
  166. [System.Environment]::GetEnvironmentVariables()
  167. "GetFolderPath: {0}" -f [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::System)
  168. [String[]] $drives = [System.Environment]::GetLogicalDrives()
  169. "GetLogicalDrives: {0}" -f [System.String]::Join(", ", $drives)
  170. #End of Script

No comments: