powershell-2.0

Issue in export Array to CSV file

試著忘記壹切 提交于 2019-12-13 08:04:39
问题 I have list of machine in text file and I am trying to get the details of physical drives, OS architecture and physical memory. With the help of Matt (SO user) here is the powershell script. $server = Get-Content .\Server.txt #$infoObject11 = @{} $infoObject11 = @{} foreach ($server in $servers) { # Gather all wmi drives query at once $alldisksInfo = Get-WmiObject -Query "SELECT * FROM Win32_DiskDrive" -ComputerName $server -ErrorAction SilentlyContinue | Group-Object __Server # Figure out

Hashtable key syntax to refer to embedded hashtable element

房东的猫 提交于 2019-12-13 06:08:23
问题 Assuming that I have a hashtable: $tokens = @{ Id=9999; Title="Lorem ipsum dolor sit amet"; Author=@{Name="John Doe"; Email='john.doe@foo.xyz'}; Analyst=@{Name="Jane Doe"; Email='jane.doe@foo.xyz'} } And a template that I would like to populate, replacing the tokens (e.g. __Title__ ) with the corresponding hashtable's value: /* Author: __Author.Name__ <__Author.Email__> Analyst: __Analyst.Name__ <__Analyst.Email__> Request: __Title__ [__Id__] */ ... Should become: /* Author: John Doe <john

How to pass a variable in the select-string of powershell

别说谁变了你拦得住时间么 提交于 2019-12-13 06:05:10
问题 There is one requirement where i need to pass a variable that is set in the powershell to the another variable Please find the below example PS D:\temp\arun> $match1=get-date -format ddMMM PS D:\temp\arun> $match1 19Jun test.txt file has the below content 19Jun1 19Jun2 19Jun3 PS D:\temp\arun> $match2=select-string -simple 19Jun test.txt PS D:\temp\arun> $match2 19Jun1 19Jun2 19Jun3 But when i am trying to pass the variable i am not getting the result. PS D:\temp\arun> $match2=select-string

What is wrong with this script to create files in all subfolders?

浪尽此生 提交于 2019-12-13 05:51:06
问题 I have a directory tree, consisting of several layers, within which I want to create 30 placeholder files, recursively in each folder. The tree looks something like this: --F:\inbox\test ----folder1 ------subfolder1 ------subfolder2 ----folder2 ------subfolder1 ------subfolder2 ----folder3 ------subfolder1 ------subfolder2 Here is what I have. $folders = gci -path f:\inbox\test -recurse | where {$_.PsIsContainer} foreach ($folder in $folders) { 1..30 | % { New-Item -Name "$_.txt" -Value (get

Retrieve users with same “City” through Get-ADuser

社会主义新天地 提交于 2019-12-13 05:48:13
问题 Is there a way by which I can retrieve a list of AD's only belonging to a particular "City"? Because, there is a property in SET-ADuser -city, so there must be a way for the reverse also! 回答1: The "City" field is mapped to an LDAP attribute called "Locality-Name". The LDAP Display Name for said property is l (that's a lowercase L): Get-ADUser "mwood" -Properties l | Select-Object Name,@{Name="City";Expression={$_.l}} It also seems that Get-ADUser is able to do the translation itself, so you

Why can't this Powershell command be executed within Jenkins?

删除回忆录丶 提交于 2019-12-13 05:44:37
问题 I have a simple powershell command to open a web application, and then input the quick-keys required to close it: #Open Application in Browser Add-Type –AssemblyName System.Windows.Forms $ie = New-Object -com "InternetExplorer.Application" $ie.Navigate("http://UrlPath") $ie.visible = $true #Quick key to exit application sleep 15 [System.Windows.Forms.SendKeys]::SendWait("%{X}") This code executes with no issue when I use powershell ISE outside Jenkins. But When I try to execute the code using

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)

Excel & PowerShell - PasteSpecial Failure With VLOOKUPs

Deadly 提交于 2019-12-13 03:02:23
问题 I have a VLOOKUP being inserted into my spreadsheet's F column like this: $vLookup = "=VLOOKUP($refCol,'$xlsLocsDIR[locs.xlsx]Device'!`$B`$2:`$C$rowsDvcs,2,FALSE)" $sheetSave.Cells.Item(2,6).Formula = $vLookup Which is, to be clear, saved properly in Excel like this: =VLOOKUP(E2,'[locs.xlsx]Device'!$B$2:$C24549,2,FALSE) (There are ~25k lines in the reference file, but there are over 200k in the file I have the VLOOKUP in.) Because of the size of the file in which I'm doing the VLOOKUP within,

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:

Best way to add results to an array across multiple functions in PowerShell?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 02:38:31
问题 I want to perform a series of checks on some infrastructure, and if the check fails, add it to a list. At the end of the workflow, write the results list. Pseudo code: Function CheckSomething { # Perform a check here. If check failed, add to the results list. } Function CheckSomethingElse { # Perform another check. If check failed, add to the results list. } Function ShowResults { $results; } CheckSomething; CheckSomethingElse; ShowResults; I would like to avoid using global variables. How