powershell-3.0

Powershell Extract Number from File Name

走远了吗. 提交于 2019-12-13 08:26:13
问题 Files names could be: 1234_billing.txt 1234billling.txt 123_billing.txt 123billing.txt How can I extract the only the number in all 4 cases? I've tried -split and $_.BaseName.Substring() but can't seem to get it correct. 回答1: Assuming that the filenames are in the array variable $flist , the following will do the trick: foreach ($file in $flist) { if ($file -match "\d+") { $matches.value } } The -match operator takes as its right operand a regex pattern; in this case we use the pattern \d+ to

Compare dates with different formats in csv file

≯℡__Kan透↙ 提交于 2019-12-13 07:41:42
问题 Whats a good way to compare dates in a csv file that looks like this: Date1,Date2,Date3 11/10/2016 9:45:00 PM,20161110,11/10/2016 11/15/2016 11:24:00 PM,20160924,11/10/2016 If a match is found, append a column like so... Date1,Date2,Date3,MatchDates 11/10/2016 9:45:00 PM,20161110,11/10/2016,Match Found 11/15/2016 11:24:00 PM,20160924,11/10/2016,No Match Found updated Trying the code that is in the comments: When comparing this with 2 of the columns... $csvFile = 'C:\Scripts\Tests\test1.csv'

Does New-Object have a memory leak?

廉价感情. 提交于 2019-12-13 07:28:05
问题 Using Powershell v3, I'm using the .net library class System.DirectoryServices.DirectoryEntry and System.DirectoryServices.DirectorySearcher to query a list of properties from users in a domain. The code for this is basically found here. The only thing you need to add, is a line $ds.PageSize=1000 in between lines $ds.Filter = '(&(objectCategory=person)(objectClass=user))' and $ds.PropertiesToLoad.AddRange($properties) . This will remove the limit of only grabbing 1000 users. The number of

A Simple Powershell Script. Why the extra “$()” in code?

馋奶兔 提交于 2019-12-13 06:07:13
问题 I have a simple script below I was looking at, but I cant understand why the writer includes the following "$($_.samAccountName)" I have tried the script with just $_.samAccountName and it works fine. Hopefully its an easy answer but why would you have the extra $() in the code? Many thanks. Get-ADGroup -Filter * | foreach { $props = [ordered] @{ GroupName = $_.Name MemberCount = Get-ADGroupMember -Identity "$($_.samAccountName)" | Measure-Object | select -ExpandProperty Count } New-Object

Powershell 3.0 Importing binary module from remote share

自古美人都是妖i 提交于 2019-12-13 04:49:13
问题 There are two machines in this scenerio: client.sub.domain.com (client machine PSRemoting to remote server) server.sub.domain.com (remote server that client is PSremoting into) I am using the below commands to start a psremote session using CredSSP to do "second-hop" authentication: $session = New-PSSession -cn server.sub.domain.com -Credential $credential -Authentication Credssp Invoke-Command -Session $session -ScriptBlock {. '\\client\Profile\Microsoft.PowerShell_profile.ps1'} Invoke

Powershell - How to select a block of text from a text based log file that has a time stamp in the log file entry

自古美人都是妖i 提交于 2019-12-13 04:43:41
问题 Sorry if the Title isn't very clear, I think part of my issue is I can't think of a way to clearly describe what I am trying to acheive, which has not helped me in 'Googling' anything that may be of help. I am trying to create a generic script / function that I can pass 2 time values, a start time and an end time that will output all log file entries between these 2 time stamps. This will become the core part of a script to extract very small time spans of log file entries from logfiles that

Code works on Powershell version 3 but not on Powershell 2

我的未来我决定 提交于 2019-12-13 04:33:37
问题 My below code works on Powershell version 3 but not on Powershell 2. when I run (Get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1).CounterSamples.CookedValue on v3 I get output but not in v2 [System.Int32] $NumberOfSamples = 3 [System.Int32] $FreeCPUThreshold = 10 [System.Double[]] $CPUArray = @() [System.Int32] $LoopCounter = 1 while ($LoopCounter -lt $NumberOfSamples) { $CPUArray += (Get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1)

Report folders which have a specific subfolder

99封情书 提交于 2019-12-13 03:48:26
问题 I am building a script which I use to deploy files to multiple specific folders. The destination folders are collected using this part. $destinations = Get-ChildItem "C:\this\is\*\my\path\" So my script replaces only if the folder has the subfolders "\my\path\" If I now check my variable it will return the fullpathes but I only need the folder name. I tried using select -path to show at least only the path but it returned as well the length, mode etc. my goal is to return only values like

How to fetch a number from a JSON file?

淺唱寂寞╮ 提交于 2019-12-13 02:55:30
问题 I am trying to extract the content from a JSON file to find the error description of a job only when the severity is set to 2, but I am encountered with following error message: Error formatting a string: Input string was not in a correct format.. At line:3 char:10 + $Sev = $("{$i`:N1}" -f $data.value.Severity) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: ({:N1}:String) [], RuntimeException + FullyQualifiedErrorId : FormatError The code which I am trying to run:

Why 'more' does not work in help cmdlet of PowerShell here?

拟墨画扇 提交于 2019-12-13 02:42:54
问题 I want to see the help contents page by page in PS. I try | more like: help about_Debuggers|more but still does not work and I get pages of info flowing at once. Is there another way or what it shte correct way to achieve this? Thanks! 回答1: 'help' is a function that wraps a Get-Help call piped to the more utility. So by default you should get paged output. When you execute your command you're actually sending output calling 'more' twice. That said, either way, it works for me. Testes on v3.