FAQ Database Discussion Community


How to get a single property value from an array of objects?

powershell,powershell-v4.0
I've an array of objects: $list = @( ` @{title='First';guid='0118B390-3AF5-406E-920D-FE140392584D'}, ` @{title='Second';guid='2C78DA61-B6EF-4E4E-8FF8-4A95D75C8188'}, ` @{title='Third';guid='0EDC361C-862E-41FC-8A60-870CADC17EC5'} ` ) I want to use the guid to look up and return a title. All I want is the value, I don't want to return an object. For example, if looking up guid '2C78DA61-B6EF-4E4E-8FF8-4A95D75C8188' I...

Passing Variables Into Statement Block Brackets In PowerShell

powershell,powershell-v3.0,powershell-v4.0
I have an array I want to pass into a variable which is then passed into a Register-ObjectEvent. It looks like the following: $body = @{ project_id = $project_id task_token = $task_token } $action = {Invoke-RestMethod -Uri http://localhost/temp.php -Method Post -Body $body} Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier MyTimer -Action...

Powershell script for Excel error code

excel,powershell,csv,export-to-csv,powershell-v4.0
I'm trying to convert an Excel .xls file that has several worksheets into a .csv with Powershell 4.0. I know the SaveAs in the for each loop isn't phrased right, and that the error is pointing to line 17 and character 9, I just don't know how to fix it...

Best practice to add a UserControl to a window in a WPF XAML PowerShell script

wpf,xaml,powershell,powershell-v4.0
I wrote the following Test.ps1 PowerShell script to display a WPF GUI: function LoadXamlFile( $path ) { [System.Xml.XmlDocument]$xml = Get-Content -Path $path $xmlReader = New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xml $xaml = [System.Windows.Markup.XamlReader]::Load( $xmlReader ) return $xaml } # Main Window $MainWindow = LoadXamlFile 'MainWindow.xaml' # Page 1 $Page1 = LoadXamlFile...

No error using wildcards when listed object does not exist

powershell,powershell-v4.0
When executing this command: get-process -name nosuchprocess, I get and error Cannot find a process with the name "nosuchprocess", and that's fine. But after this one: get-process -name nosuchprocess*, there's no error, no output - just empty prompt. The same behaviour can be observed with other commands (get-item, get-childitem, select-string)....

PowerShell master script to execute functions

powershell-ise,powershell-v4.0
I have a.ps1file which houses 4 functions. I am not sure how to call the functions or have that script run. I need all the 4 functions to run one after the other and i believe i have tried every possible way to execute that powershell file, but haven't got...

Import-CSV cares that I have duplicate members, but I need the duplicates

powershell,powershell-v4.0
I'm writing a simple powershell script to open a CSV which is generated by me and push each element into a telnet session to provision devices. However, my script won't even start due to the fact that I have duplicate members! If anyone has guidance on this please let me...

executing powershell with a brackets in argument string

c#,powershell,powershell-v4.0
We have a powershell script which we are shelling from a c# .Net 4.5 program on systems with powershell 4. One of its main purposes is to find and stop an associated windows service so we can maintain it. The problem is on some sites the name of the service...

Full output hidden on console

powershell,powershell-v4.0
I don't get full output of the following code I made. For Example: DriveSpace : {174, 0, 98, 171...} Notice the ellipses (...) after 171. It is skipping the rest of the output after that. You can run the following script to see my output. #Start of script $cpu =...

Write an entire PowerShell property value to the console

powershell,powershell-v4.0
Select-Object is currently truncating the result as follows. > Invoke-WebRequest "http://www.wikipedia.org" | select content Content ------- <!DOCTYPE html>... How do we write the entire result to the screen?...

How do I run PowerShell 4.0 after upgrading from PowerShell 2.0?

powershell,powershell-v2.0,powershell-v4.0,chocolatey
I have installed DotNet 4.5.2 and PowerShell 4 on my Windows 7 SP 1 workstation using chocolatey: choco install dotnet4.5.2 -y choco install powershell -y They installed without errors and appear in the choco list: choco list --local-only DotNet4.5.2 4.5.2.20140902 PowerShell 4.0.20141001 I previously had PowerShell 2.0 installed, and when...

Rename folders in file share

powershell,powershell-v4.0
The script is comparing folder names in a share where we store user profiles to those in AD and seeing which ones are disabled or don't exist and counting them. I now want to take that list and rename all the user folders that aren't in AD to .old. $delusercount...

Passing in an empty set throws an error

powershell,set,hashset,powershell-v4.0
I'm trying to use a HashSet in my powershell script as per this answer. Add-Type -AssemblyName System.Core $set= new-object 'System.Collections.Generic.HashSet[string]' But if I make a function which expects such a set function foo([Parameter(Mandatory=$true)][Collections.Generic.HashSet[string]]$set){ and pass in the set foo($set) I get an error if $set is empty: Cannot bind argument...

how to use attributes values in powershell script

powershell,chef,chef-recipe,powershell-v4.0
I am using powershell_script resource in chef to create a database in sqlserver 2012 here. I have used database name as test1 hardcoded in script. Now I want to provide the database name from the attribute file. How can I get the value referenced from attribute file to the script....

Running function as a job in powershell

powershell,powershell-v3.0,powershell-v4.0
Below is the funtion for just pinging all the machines from a win-form appliaction: I am just pinging all the machines and displaying result in output box and saving it into a csv file: As the UI running as Single thread mode, I am not able to perform any other...

How to pass a custom enum to a function in powershell

powershell,enums,powershell-v2.0,powershell-v4.0,pscmdlet
When defining a function, how can you reference a custom enum? Here's what I'm trying: Add-Type -TypeDefinition @" namespace JB { public enum InternetZones { Computer ,LocalIntranet ,TrustedSites ,Internet ,RestrictedSites } } "@ -Language CSharpVersion3 function Get-InternetZoneLogonMode { [CmdletBinding()] param ( [Parameter(Mandatory=$true)] [JB.InterfaceZones]$zone ) [string]$regpath = ("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\{0}" -f [int]$zone)...

how to access hashtable values in powershell if hashtable is generated in a function?

powershell,powershell-v4.0
I have created a simple GUI to enter some values that are stored in a .txt file as soon as the User clicks OK. For verification I'm displaying the data of the just created file with its input in a popup window. As I want to use the data of...

How can I write an if statement using a variable in a foreach loop using Powershell?

powershell,powershell-v4.0
I'm running the foreach loop below in my code right now, and it works great. However, I'd prefer to have it send one single email at the end of the script if the condition is met, instead of an email for each problem found. How can I do this? foreach...

Access UserControl elements/properties from main window in a WPF XAML PowerShell script

wpf,xaml,powershell,powershell-v4.0
I wrote the following Test.ps1 PowerShell script to display a WPF GUI: function LoadXamlFile( $path ) { [System.Xml.XmlDocument]$xml = Get-Content -Path $path $xmlReader = New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xml $xaml = [System.Windows.Markup.XamlReader]::Load( $xmlReader ) return $xaml } # Main Window $MainWindow = LoadXamlFile 'MainWindow.xaml' # Page 1 $Page1 = LoadXamlFile...

How to tell when xml element is one of multiple or not in powershell

powershell,powershell-v4.0
I have two powershell scripts: One using a single <b> element: $x = [xml] "<a><b>foo</b></a>" $x.a.b.length And one using multiple (two in this case) <b> elements: $y = [xml] "<a><b>foo</b><b>bar</b></a>" $y.a.b.length The first script returns the length of the text in the one <b> element, the second returns the number...

Find numbers after specific text in a string with RegEx

regex,string,powershell,string-matching,powershell-v4.0
I have a multiline string like the following: 2012-15-08 07:04 Bla bla bla blup 2012-15-08 07:05 *** Error importing row no. 5: The import of this line failed because bla bla 2012-15-08 07:05 Another text that I don't want to search... 2012-15-08 07:06 Another text that I don't want to...

tee with utf-8 encoding

powershell,encoding,tee,powershell-v4.0
I'm trying to tee a server's output to both the console and a file in Powershell 4. The file is ending up with a UTF-16 encoding, which is incompatible with some other tools I'm using. According to help tee -full: Tee-Object uses Unicode enocding when it writes to files. ......

Group-Object diffencies with or without code block

powershell,powershell-v4.0
The code below produce 2 "identical" Hashtables, however on the one that was grouped using a code block I can't get items from the key. $HashTableWithoutBlock = Get-WmiObject Win32_Service | Group-Object State -AsHashTable $HashTableWithBlock = Get-WmiObject Win32_Service | Group-Object {$_.State} -AsHashTable Write-Host "Search result for HashTable without using code block...

How to hide Visio 2013 when using interop assembly in PowerShell

powershell,office-interop,visio,powershell-v4.0,visio2013
I'm willing to run Visio 2013 silently through PowerShell. At the moment I use the following code: Add-Type -Path 'Microsoft.Office.Interop.Visio.dll' $visio = New-Object Microsoft.Office.Interop.Visio.ApplicationClass $visio.Visible = $false $visio.Quit() The code works but I briefly see the Visio splash screen before it is hidden. I would like to create the application...