powershell-2.0

Extract the filename from a path

耗尽温柔 提交于 2019-12-17 16:26:39
问题 I want to extract filename from below path: D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv Now I wrote this code to get filename. This working fine as long as the folder level didn't change. But in case the folder level has been changed, this code need to rewrite. I looking a way to make it more flexible such as the code can always extract filename regardless of the folder level. ($outputFile).split('\')[9].substring(0) 回答1: If you are ok with including the extension this

Calling generic static method in PowerShell

血红的双手。 提交于 2019-12-17 16:15:04
问题 How do you call a generic static method of a custom class in Powershell? Given the following class: public class Sample { public static string MyMethod<T>( string anArgument ) { return string.Format( "Generic type is {0} with argument {1}", typeof(T), anArgument ); } } And this is compiled into an assembly 'Classes.dll' and loaded into PowerShell like this: Add-Type -Path "Classes.dll" What's the easiest way to call the MyMethod method? 回答1: You can call generic methods, refer to the post

How do I write to standard error in PowerShell?

放肆的年华 提交于 2019-12-17 15:39:30
问题 I'm having trouble figuring out how to both echo to the standard error stream and redirect the error stream of an executable. I have come from a Bourne shell and Korn shell background, of which I would use; # Write to stderr echo "Error Message!" >&2 # Redirect stderr to file /do/error 2>/tmp/err.msg 回答1: Use Write-Error to write to stderr. To redirect stderr to file use: Write-Error "oops" 2> /temp/err.msg or exe_that_writes_to_stderr.exe bogus_arg 2> /temp/err.msg Note that PowerShell

How to execute powershell commands from a batch file?

回眸只為那壹抹淺笑 提交于 2019-12-17 15:28:47
问题 I have a PowerShell script to add a website to a Trusted Sites in Internet Explorer: set-location "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" set-location ZoneMap\Domains new-item TESTSERVERNAME set-location TESTSERVERNAME new-itemproperty . -Name http -Value 2 -Type DWORD I want to execute these PowerShell commands from a batch file. It seems simple when I have to run a single command, BUT in this case I have a sequence of related commands. I want to avoid creating a

How to check Network port access and display useful message?

依然范特西╮ 提交于 2019-12-17 15:25:23
问题 I was trying to check whether the port is opened or not using powershell like follows. (new-object Net.Sockets.TcpClient).Connect("10.45.23.109", 443) This method works , but the output is not user-friendly. It means if there are no errors then it has access. Is there any way to check for success and display some message like " Port 443 is operational"? 回答1: If you're running Windows 8/Windows Server 2012 or newer, you can use the Test-NetConnection command in PowerShell. Ex: Test

What security setting is preventing Remote PowerShell 2.0 from accessing UNC paths

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 10:34:44
问题 This is just crazy, I am starting on PowerShell. And of course I need to do Admin work remotely. A simple dir \\server\share\folder Just refuses to work, I get this error Get-ChildItem : Cannot find path '\\server\share\folder' because it does not exist. + CategoryInfo : ObjectNotFound: (\\server\share\folder:String) [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand To me it is pretty obvious it is an access rights

When should I use Write-Error vs. Throw? Terminating vs. non-terminating errors

拥有回忆 提交于 2019-12-17 08:14:38
问题 Looking at a Get-WebFile script over on PoshCode, http://poshcode.org/3226, I noticed this strange-to-me contraption: $URL_Format_Error = [string]"..." Write-Error $URL_Format_Error return What is the reason for this as opposed to the following? $URL_Format_Error = [string]"..." Throw $URL_Format_Error Or even better: $URL_Format_Error = New-Object System.FormatException "..." Throw $URL_Format_Error As I understand, you should use Write-Error for non-terminating errors, and Throw for

Comparing array variables in PowerShell

风格不统一 提交于 2019-12-17 07:30:20
问题 I have this script to compare two folders. $firstfolder = Get-ChildItem C:\firstfolder $secondfolder = Get-ChildItem C:\firstfolder if ($firstfolder -eq $secondfolder) { Write-Host "Folders are the same." } else { Write-Host "Error: Doesn't match." } As you can see, I compare the same folder. Problem is, that it will never consider, that the arrays are equal. Why? 回答1: In PowerShell, variables that point to arrays are evaluated in expressions by enumerating the contents of the arrays

How to properly -filter multiple strings in a PowerShell copy script

元气小坏坏 提交于 2019-12-17 07:12:32
问题 I am using the PowerShell script from this answer to do a file copy. The problem arises when I want to include multiple file types using the filter. Get-ChildItem $originalPath -filter "*.htm" | ` foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); ` New-Item -ItemType File -Path $targetFile -Force; ` Copy-Item $_.FullName -destination $targetFile } works like a dream. However, The problem arises when I want to include multiple file types using the filter. Get

Using Invoke-Command -ScriptBlock on a function with arguments

谁都会走 提交于 2019-12-17 07:09:59
问题 I'm writing a PowerShell script that will execute commands on a remote host using Invoke-Command and its -ScriptBlock parameter. For example, function Foo { ... return "foo" } $rv = Invoke-Command --Credential $c --ComputerName $fqdn -ScriptBlock ${function:Foo} This works fine. What I'd like to do now is the same thing, but call a function with local arguments. For example, function Bar { param( [String] $a, [Int] $b ) ... return "foo" } [String] $x = "abc" [Int] $y = 123 $rv = Invoke