powershell-4.0

How can I Start-Process powershell.exe with some string splitter?

寵の児 提交于 2020-01-07 03:01:05
问题 I want to start powershell.exe with a scriptblock like this (it's working fine): Start-Process powershell.exe { Get-Help Get-Process } But this script doesn't work: Start-Process powershell.exe { $string = "123xAbcxEFG" $split1,$split2,$split3 = $string.Split("x") Write-Output $split1 Write-Output $split2 Write-Output $split3 sleep 10 } I think I need Add-Type -AssemblyName "SomeNameSpace" , but how can I find that namespace? Any intellisense or something like this? 回答1: you can use start-job

How to set share permissions on a remote share with Powershell?

ε祈祈猫儿з 提交于 2020-01-06 19:49:14
问题 I need to set the share permissions of a remote share from a Powershell 4 script. I've looked at this page, specifically the command Grant-SmbShareAccess but that cmdlet sets permissions on local shares, I would love to have seen a -ComputerName parameter but, alas, there isn't one. I want to do something like: Grant-SmbShareAccess -ComputerName MYREMOTESERVER -Name <share_name> -AccountName <domain_account> -AccessRight Full Any ideas on how to do this? My remote server could be a Windows

Why can't I return a value of type System.Collections.Specialized.NameValueCollection? [duplicate]

六月ゝ 毕业季﹏ 提交于 2020-01-06 17:01:00
问题 This question already has answers here : PowerShell changes return object's type (2 answers) Closed 4 years ago . I can create a new instance of the [System.Collections.Specialized.NameValueCollection] type, and it seems to work fine. But if I do it inside a function, and then return the object, the caller receives either a [String] (if the collection contains only a single item) or [Object[]] if it contains multiple items (and then each item in that array is a [String] representing one of

Filter out users that contains a word or character with Get-ADUser

大城市里の小女人 提交于 2020-01-06 15:03:45
问题 I am trying to get a list of AD-Users in different domains but cannot find a way to exclude users that has "Health" in their name for an example. This is how my query looks like as of now: #$excludedusers = @('HealthMailbox123456') Get-ADUser -Server $test -Credential $1cred -Filter{enabled -eq $true} | Where-Object { $_.DistinguishedName -notlike '*OU=.Service Accounts,*' } | Select-object Samaccountname,surname,givenname | Where { $excludedusers -NotContains$_.Samaccountname } As of now,

PowerShell executes the wrong exception handler

假如想象 提交于 2020-01-06 14:46:18
问题 I think I have found a nasty bug in PowerShell 4.0 exception handling but I'm new to PowerShell scripting and want to be sure I didn't miss something. I managed to find a code that reproduces the problem: foreach ($id in 1..20) { try { # The objective is to generate basic exceptions and see how they are caught throw "#$id" } # Oddly, if the following block is removed or commented, there is no problem catch [System.ArithmeticException] { Write-Host ("[ArithmeticException] {0}" -f $_.Exception

StartType not being listed when using Get-Service

泄露秘密 提交于 2020-01-06 06:46:22
问题 I have a script that checks a list of computer names for a list of known services. It gives the status of those services and it also is supposed to list the StartType. Is there a reason why the StartType is not being given in the output? In the output, the PSComputerName, ServiceName, and Status columns contain data but the StartType column remains blank. $myServices = $PSScriptRoot + "\services.txt" # $PSScriptRoot references current directory $myServers = $PSScriptRoot + "\servers.txt" $Log

XPath search on an attribute value in PowerShell

烈酒焚心 提交于 2020-01-06 02:24:24
问题 Despite the examples I've seen online, it doesn't seem to be possible to do an XPath search on an attribute value in PowerShell. [xml]$xml = '<a><b><c foo="bar"></c></b></a>' $xml | select-xml -xpath //c[@foo] # This returns a node $xml | select-xml -xpath //c[@foo='bar'] # This does not I've never been so stumped by something so simple. :-) How can I get this to work? 回答1: If you quote the xpath it will work fine: [xml]$xml = '<a><b><c foo="bar"></c></b></a>' $xml | select-xml -xpath "//c[

How to break Foreach loop in Powershell?

雨燕双飞 提交于 2020-01-04 09:15:29
问题 I'm looking for a way to break my Foreach loop. This is my code $MyArray = @("desktopstudio","controller","storefront","desktopdirector","licenseserver") $component = "toto,blabla" $component = $component.Split(",") foreach ($value in $component) { if ($MyArray -notcontains $value) { Write-host "Your parameter doesn't match" Write-host "Please use one of this parameter $MyArray" Break } } write-host "I'm here" I don't understand why it's not breaking my code, because this is the result when I

How to break Foreach loop in Powershell?

非 Y 不嫁゛ 提交于 2020-01-04 09:15:20
问题 I'm looking for a way to break my Foreach loop. This is my code $MyArray = @("desktopstudio","controller","storefront","desktopdirector","licenseserver") $component = "toto,blabla" $component = $component.Split(",") foreach ($value in $component) { if ($MyArray -notcontains $value) { Write-host "Your parameter doesn't match" Write-host "Please use one of this parameter $MyArray" Break } } write-host "I'm here" I don't understand why it's not breaking my code, because this is the result when I

How do I get the number of distinct files in a directory in powershell?

▼魔方 西西 提交于 2020-01-03 18:01:04
问题 I currently have this code which returns the total number of files but I don't want to count multiple files in this number count? Lets say I have this: 01Red.txt 01Blue.txt 02Red.txt 05Red.txt 05Green.txt Get-ChildItem -File *.txt -Path "C:\Users\Test\Desktop\TestDirectory" | Measure-Object | %{$_.Count} I want to return a total count of 3 based on 01,02,05 but with my code I get 5. How can I get it to return 3 and ignore everything past the first 2 characters in the string? 回答1: I might