powershell-2.0

Quick way to convert CSV types to original types with PowerShell's Import-Csv cmdlet?

不打扰是莪最后的温柔 提交于 2019-12-06 06:23:47
I'm using PowerShell v2 and have a custom type I've defined called Material using code like this: add-type @" public struct Material { public string BusinessUnit; public string Source; public string PrimarySource; public string LegacyMaterialNumber; } "@ ...and I've got a collection of objects of this type I'm writing to a CSV file using the Export-Csv cmdlet. That works fine and produces data with the original type specified like this: #TYPE Material BusinessUnit,Source,PrimarySource,LegacyMaterialNumber BU1,NAFO,NAFO-FG,17502 BU1,NAFO,NAFO-FG,17504 BU1,NAFO,NAFO-FG,17739 BU1,NAFO,NAFO-FG

Assigning an array to a dictionary entry in Powershell

心不动则不痛 提交于 2019-12-06 06:07:40
问题 I am trying to assign an array as a value to a dictionary entry as follows but its throwing exception. $sd = New-Object 'System.Collections.Generic.SortedDictionary[int, string[]]' $sd[0] = "Data1a", "asda"; Any idea? 回答1: Use cast to [string[]] : $sd = New-Object 'System.Collections.Generic.SortedDictionary[int, string[]]' $sd[0] = [string[]]("Data1a", "asda") Another options is to change the dictionary value type to object[] : $sd = New-Object 'System.Collections.Generic.SortedDictionary

Closing All Explorer Windows in PowerShell

浪尽此生 提交于 2019-12-06 04:19:15
问题 I am writing the following code to close all explorer windows with PowerShell: (New-Object -comObject Shell.Application).Windows() | ? { $_.FullName -ne $null} | ? { $_.FullName.toLower().Endswith('\explorer.exe') } | % { $_.Quit() } But it does not close out all the open windows. Instead, it closes only RoundDown(N/2)+1 windows, and leaves RoundUp(N/2)-1 windows open. Can anyone help with this? 回答1: I think there's something in the pipeline that goes wrong. This code works: $a = (New-Object

How can you set a time limit for a PowerShell script to run for?

安稳与你 提交于 2019-12-06 03:17:34
问题 I want to set a time limit on a PowerShell (v2) script so it forcibly exits after that time limit has expired. I see in PHP they have commands like set_time_limit and max_execution_time where you can limit how long the script and even a function can execute for. With my script, a do/while loop that is looking at the time isn't appropriate as I am calling an external code library that can just hang for a long time. I want to limit a block of code and only allow it to run for x seconds, after

Powershell - SetForegroundWindow

血红的双手。 提交于 2019-12-06 02:21:58
With a powershell code I try to change the position of a window (is works correctly) and put this windows "Always on top". Please find below my code: Import-Module C:/install/WASP/wasp.dll for($i=1; $i -le 300000; $i++) { $allWindow = Select-Window MyCheck* if($allWindow) { foreach ($currentWindow in $allWindow) { $positionWindow = WindowPosition $currentWindow foreach ($currentPosition in $positionWindow) { #if we find the correct windows if ( $currentWindow.title -match "\([0-9]*\)#" ) { #write-host "@@##@@"$currentWindow.title",(@@#@@)"$currentPosition.x",(@@#@@)"$currentPosition.y",(@@#@@)

Using powershell to find files that match two seperate regular expressions

折月煮酒 提交于 2019-12-06 00:33:27
I want to find any file that can match two regular expressions, for example all files that have at least one of bat , hat , cat as well as the word noun . That would be any file that can match both regular expressions [bhc]at and noun . The regex parameter to select-string only seems to work on a line-by-line basis. It seems you can pass in multiple patterns delimited by commas ( select-string -pattern "[bhc]at","noun" ) but it matches either, rather than both. You can always just use two filters: dir | ? {(gc $_) -match '[bhc]at'} | ?{(gc $_) -match 'noun'} This just gets all the objects that

Compiling new type in PowerShell v2 - Cookie Aware WebClient

夙愿已清 提交于 2019-12-06 00:22:29
问题 I am trying to compile a "cookie aware" version of the WebClient class - but I can't seem to get over some of the hurdles of using the Add-Type cmdlet added in PowerShell v2. Here is the code I am trying to compile: PS C:\> $def = @" public class CookieAwareWebClient : System.Net.WebClient { private System.Net.CookieContainer m_container = new System.Net.CookieContainer(); protected override System.Net.WebRequest GetWebRequest(System.Uri address) { System.Net.WebRequest request = base

Powershell To Check Local Admin Credentials

自古美人都是妖i 提交于 2019-12-05 20:33:32
问题 I'm trying to run a script that requires Administrator input in order to process certain things. Rather than have the script run unsuccessfully I'm trying to trap the error and throw it back into the Credentials, but I can't find a command I can pass Local Admin Credentials with to a Trap. Does anyone have anything that might work? I've found MANY that will check domain credentials, but this is a LOCAL Admin account. To clarify, I am using: $Cred = Get-Credential I need to verify the output

Powershell array scope; why is my array empty?

拜拜、爱过 提交于 2019-12-05 18:43:00
I want to have an array and add elements to it from different functions in my script. My example below illustrates where I might be misunderstanding something regarding scope. My understanding currently is that if an array is defined outside the function, and then elements are added inside the function, those elements should be available outside the function. Function ListRunningServices { $services+= Get-Service | ?{$_.Status -eq "Running"} | sort Name | select Name; } $services = @(); ListRunningServices; $services; What am I missing here? Perhaps my style is completely wrong. The $services

How to set LastWriteTime property of a file?

有些话、适合烂在心里 提交于 2019-12-05 18:28:45
I would like to change the creation date of the files that I generate with this script : $clientname = Read-Host "Enter the client name" $path = Read-Host "Enter the complete path of .bak files" $time = "01-00-00" $space = " " for ($i = 0; $i -lt 7;$i++) { $date = (Get-Date).AddDays(-1-$i).ToString('yyyy-MM-dd') New-Item -ItemType file $path$clientname$space$date$space$time.bak } So it gives me theses files : Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 16/08/2013 16:55 0 client 2013-08-15 01-00-00.bak -a--- 16/08/2013 16:55 0 client 2013-08-14 01-00-00.bak -a--- 16/08