powershell-2.0

Does anyone have a Dependency graph and topological sorting code snippet for PowerShell?

落爺英雄遲暮 提交于 2019-12-04 11:43:57
We were trying to parallelize our build with dependency graph and topological sorting. All our build logic is written in Msbuild and we are using powershell to call it. Has any one implemented dependency graph and topological sorting using powershell? I know that in unix there is a utility called tsort available. Is there any thing similar available in powershell? This article gives good idea but it is done in C# " http://msdn.microsoft.com/en-us/magazine/dd569760.aspx" I did a quick translation of Wikipedia's Topological Sort Algorithm : function Get-TopologicalSort { param( [Parameter

How to remote execute an ELEVATED remote script in PowerShell

微笑、不失礼 提交于 2019-12-04 11:23:36
问题 I have two servers: serverA (windows 2003 server) serverB (windows 7) ServerA contains a folder with a batch file (deploy.bat) that needs to be executed from an elevated powershell prompt. In ServerA , if I run it from a normal prompt or powershell prompt it fails. If I run it from an elevated prompt it works. (run as administrator). The problem I have is when I try to execute batch file from serverB using a remote powershell execution. I am able to execute with this command: Invoke-Command

Assigning an array to a dictionary entry in Powershell

江枫思渺然 提交于 2019-12-04 11:21:11
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? 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[int, object[]]' $sd[0] = "Data1a", "asda" 来源: https://stackoverflow.com/questions/4658759/assigning-an-array

Closing All Explorer Windows in PowerShell

China☆狼群 提交于 2019-12-04 11:18:05
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? I think there's something in the pipeline that goes wrong. This code works: $a = (New-Object -comObject Shell.Application).Windows() | ? { $_.FullName -ne $null} | ? { $_.FullName.toLower().Endswith('

How to create real objects with behavior (methods) in PowerShell?

元气小坏坏 提交于 2019-12-04 10:34:33
问题 Probably this question has been answered before.... but I have not found a specific answer to my needs. BTW I'm using PowerShell 3 Well, I'm new in PowerShell but I have a lot of experience as a C# developer, so working with objects is very important to me. So I'm wondering if there's a clean way to apply OOP concepts (well not all of them, though that would be awesome) in a PowerShell script, for example there are specific things that I would like to do. Note: I'm aware that I can write C#

Powershell - Loop script until user chooses to exit

▼魔方 西西 提交于 2019-12-04 09:25:12
How can I start a script over again? I have 3 switches and I want them to revert back to the beginning of the script. Import-Module ActiveDirectory Write-Host "--Please Login using a.account--" #login $credential = Get-Credential #Main Write-Host "--Remote Computer Rename v2.0--" Write-Host "1. Query AD (Outputs to a text file)" Write-Host "2. Quick computer rename" Write-host "3. Quit" $choice=Read-Host "Chose a number to continue" #AD Query for computer switch ($choice) { 1 { Write-Host "--Enter first five characters of computer name or full computer name i.e. USCLT--" $cn=Read-Host

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

若如初见. 提交于 2019-12-04 08:21:25
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 which I will terminate that code block and return a response to the user that the script timed out. I

What objects are suitable for Add-Member?

Deadly 提交于 2019-12-04 07:36:12
Documentation states: Adds a user-defined custom member to an instance of a Windows PowerShell object. What "Windows PowerShell object" stands for? This works fine: $obj = new-object system.object $obj | add-member -membertype noteproperty -name Name -value "OK" $obj.name But this does not: $obj = @{} Actually, I am trying to add property to $error[0]. PowerShell has what's called a PSObject that is a wrapper around any .NET object (or it can be a totally custom object) and when you call Add-Member, PowerShell is implicitly wrapping the real .NET object with a PSObject. The way Add-Member

How to find the Windows version from the PowerShell command line

让人想犯罪 __ 提交于 2019-12-04 07:25:33
问题 How do I find which Windows version I'm using? I'm using PowerShell 2.0 and tried: PS C:\> ver The term 'ver' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify tha t the path is correct and try again. At line:1 char:4 + ver <<<< + CategoryInfo : ObjectNotFound: (ver:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException How do I do this? 回答1: Since you have

specific text of today's date from txt file by powershell

守給你的承諾、 提交于 2019-12-04 05:52:10
问题 I have a text file. Similar to this. This is a sample data. This is a sample data. This is a sample data. Sat Jun 06 08:17:01 2015 WARNING: Cannot delete file. Error-101 Error-100 Error-102 This is a sample data. This is a sample data. Error-10666 This is a sample data. Sat Jun 06 10:17:01 2015 File deleted. This is a sample data. This is a sample data. Sat Jun 06 10:17:01 2015 File deleted. Sat Jun 06 11:17:01 2015 WARNING: Cannot delete file. Error-101 This is a sample data. Sat Jun 06 18