powershell-5.0

Why can't I use predefined variables in class methods?

北城以北 提交于 2020-12-10 14:22:53
问题 I tried to use predefined variables like $PSVersionTable or $PSScriptRoot in a class method. They failed with the error message Variable is not assigned in the method. Example: Class Foo { [String]$Version GetVersion() { If ($PSVersionTable) { $this.Version = $PSVersionTable.PSVersion } } } But why? 回答1: Class Foo { [String] $Version GetVersion() { if ($global:PSVersionTable) { $this.Version = $global:PSVersionTable.PSVersion } } } $foo = [Foo]::new() $foo.GetVersion() Write-Host $foo.Version

Why can't I use predefined variables in class methods?

旧城冷巷雨未停 提交于 2020-12-10 14:20:36
问题 I tried to use predefined variables like $PSVersionTable or $PSScriptRoot in a class method. They failed with the error message Variable is not assigned in the method. Example: Class Foo { [String]$Version GetVersion() { If ($PSVersionTable) { $this.Version = $PSVersionTable.PSVersion } } } But why? 回答1: Class Foo { [String] $Version GetVersion() { if ($global:PSVersionTable) { $this.Version = $global:PSVersionTable.PSVersion } } } $foo = [Foo]::new() $foo.GetVersion() Write-Host $foo.Version

Why can't I use predefined variables in class methods?

亡梦爱人 提交于 2020-12-10 14:10:29
问题 I tried to use predefined variables like $PSVersionTable or $PSScriptRoot in a class method. They failed with the error message Variable is not assigned in the method. Example: Class Foo { [String]$Version GetVersion() { If ($PSVersionTable) { $this.Version = $PSVersionTable.PSVersion } } } But why? 回答1: Class Foo { [String] $Version GetVersion() { if ($global:PSVersionTable) { $this.Version = $global:PSVersionTable.PSVersion } } } $foo = [Foo]::new() $foo.GetVersion() Write-Host $foo.Version

Why can't I use predefined variables in class methods?

扶醉桌前 提交于 2020-12-10 14:02:02
问题 I tried to use predefined variables like $PSVersionTable or $PSScriptRoot in a class method. They failed with the error message Variable is not assigned in the method. Example: Class Foo { [String]$Version GetVersion() { If ($PSVersionTable) { $this.Version = $PSVersionTable.PSVersion } } } But why? 回答1: Class Foo { [String] $Version GetVersion() { if ($global:PSVersionTable) { $this.Version = $global:PSVersionTable.PSVersion } } } $foo = [Foo]::new() $foo.GetVersion() Write-Host $foo.Version

Mapped network drives are not showing in My Computer

…衆ロ難τιáo~ 提交于 2020-08-07 05:38:42
问题 I am trying to create a external network drive using PowerShell 5.0. I Need those drive to display in My Computer. For that purpose I am using this follow command. New-PSDrive -Name "X" -PSProvider FileSystem -Root "\\192.168.0.1\hde_path" -Persist Is there anything wrong with this command? Because as per my understanding if I use -Persist it is should be displayed in the My Computer window. After using this, the mapped drive X: is not being displayed in My Computer. Thanks in Advance! 回答1:

Mapped network drives are not showing in My Computer

六月ゝ 毕业季﹏ 提交于 2020-08-07 05:38:22
问题 I am trying to create a external network drive using PowerShell 5.0. I Need those drive to display in My Computer. For that purpose I am using this follow command. New-PSDrive -Name "X" -PSProvider FileSystem -Root "\\192.168.0.1\hde_path" -Persist Is there anything wrong with this command? Because as per my understanding if I use -Persist it is should be displayed in the My Computer window. After using this, the mapped drive X: is not being displayed in My Computer. Thanks in Advance! 回答1:

Using custom type in Powershell 5.0 class [duplicate]

倖福魔咒の 提交于 2020-07-23 04:41:33
问题 This question already has answers here : Using .Net Objects within a Powershell (V5) Class (3 answers) Closed 4 years ago . Here is example code that is causing me lots of headaches at the moment. if (("Win32.NativeMethods" -as [type]) -eq $null){ Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); ' -name NativeMethods -namespace Win32 } class AppInstance { [string]$App = 'Notepad.exe' [IntPtr]$hWnd = 0 [System.Object]

Using custom type in Powershell 5.0 class [duplicate]

a 夏天 提交于 2020-07-23 04:41:04
问题 This question already has answers here : Using .Net Objects within a Powershell (V5) Class (3 answers) Closed 4 years ago . Here is example code that is causing me lots of headaches at the moment. if (("Win32.NativeMethods" -as [type]) -eq $null){ Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); ' -name NativeMethods -namespace Win32 } class AppInstance { [string]$App = 'Notepad.exe' [IntPtr]$hWnd = 0 [System.Object]

Using custom type in Powershell 5.0 class [duplicate]

两盒软妹~` 提交于 2020-07-23 04:39:18
问题 This question already has answers here : Using .Net Objects within a Powershell (V5) Class (3 answers) Closed 4 years ago . Here is example code that is causing me lots of headaches at the moment. if (("Win32.NativeMethods" -as [type]) -eq $null){ Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); ' -name NativeMethods -namespace Win32 } class AppInstance { [string]$App = 'Notepad.exe' [IntPtr]$hWnd = 0 [System.Object]

List file names in a folder matching a pattern, excluding file content

末鹿安然 提交于 2020-06-09 16:50:08
问题 I am using the below to recursively list all files in a folder that contains the $pattern Get-ChildItem $targetDir -recurse | Select-String -pattern "$pattern" | group path | select name But it seems it both list files having the $pattern in its name and in its content , e.g. when I run the above where $pattern="SAMPLE" I get: C:\tmp\config.include C:\tmp\README.md C:\tmp\specs\SAMPLE.data.nuspec C:\tmp\specs\SAMPLE.Connection.nuspec Now: C:\tmp\config.include C:\tmp\README.md indeed contains