powershell-2.0

List of all colors available for PowerShell?

≯℡__Kan透↙ 提交于 2019-12-09 05:20:52
问题 I am searching for a list of all colors I can use in PowerShell. Since we need to provide names and no hexnumbers, it's hard to figure out if a color exists or not, at least if you don't know how :)) For example, as -foregroundcolor write-host "hello world" -foregroundcolor "red" 回答1: The console colors are in an enum called [System.ConsoleColor]. You can list all the values using the GetValues static method of [Enum] [Enum]::GetValues([System.ConsoleColor]) or just [Enum]::GetValues(

How to convert string to table or objects in powershell

泪湿孤枕 提交于 2019-12-09 03:39:40
问题 How can I convert the below exported text to csv so that i can use it as objects in powershell. Eg: where{$_.qlimit -eq 27} Text: samid qlimit qused Administrator -1 0 Guest -1 0 admin 27 8 krbtgt -1 0 r -1 0 admin2 44 0 回答1: Use the Get-Content cmdlet to load the file, replace two or more whitespaces with a comma and convert it to CSV using the ConvertFrom-Csv cmdlet: $object = (Get-Content 'your_file').Trim() -replace '\s{2,}', ',' | ConvertFrom-Csv If you now query your object: $object |

Find character position and update file name

天涯浪子 提交于 2019-12-09 02:23:33
问题 What function might I use to find a character position in a string using PowerShell 2.0. i.e I would use CHARINDEX or PATINDEX if using SQL Server. I looked at using the Select-String cmdlet but it doesn't seem to do what I need it to do. Ultimately I'm looking to find a "_" character in a file name and strip off everything to the following "." . Example file name 237801_201011221155.xml Final Solution, The below strips out all characters from and including <_> to <.> for all .xml files in

show just one column in result?

不羁岁月 提交于 2019-12-08 19:28:09
问题 Bit of a simple question but how do I select a specific column in the below code? I only want to show TIME column and nothing else. I try and put in FORMAT_TABLE TIME but it just populates with TIME multiple times without actually showing the time.. $server_event = Get-Content -Path "c:\Temp\Servers.txt" foreach($servers in $server_event) { $event = Get-EventLog -computerName $servers -logname system | Where-Object {$_.EventID -eq 6009} | Select -First 1 $event } RESULT: I ONLY WANT TO SHOW

Powershell - Removing all duplicate entries

跟風遠走 提交于 2019-12-08 19:21:23
I am trying to find a Powershell command line that will read in a text file remove all duplicated lines (2+) and retain none of the duplicated lines. I haven't been able to find an answer for my question anywhere on Stackoverflow nor anywhere else. Every example I have found so far only shows removing one and/or many of the duplicated lines and retaining one. Is this possible through Powershell 2.0? PowerShell Command Example: Get-Content "C:\Temp\OriginalFile.txt" | select -unique| Out-File "C:\Temp\ResultFile.txt" OriginalFile.txt 1 1 1 2 2 3 4 ResultFile.txt (Actual) 1 2 3 4 ResultsFile.txt

Parameters with default value not in PsBoundParameters?

左心房为你撑大大i 提交于 2019-12-08 16:04:35
问题 General code Consider this code: PS> function Test { param($p='default value') $PsBoundParameters } PS> Test 'some value' Key Value --- ----- p some value PS> Test # nothing I would expect that $PsBoundParameters would contain record for $p variable on both cases. Is that correct behaviour? Question I'd like to use splatting that would work like this for a lot of functions: function SomeFuncWithManyRequiredParams { param( [Parameter(Mandatory=$true)][string]$p1, [Parameter(Mandatory=$true)]

Powershell argument passing to function seemingly not working

▼魔方 西西 提交于 2019-12-08 15:41:33
问题 I sense that I am doing something silly, but here is the issue: Function getPropertyOfFile($a, $b, $c) { $a.GetDetailsOf($b, $c) } If I pass $a, $b, $c variables that are appropriate to the function, it fails saying that "Method invocation failed because [System.Object[]] doesn't contain a method named 'GetDetailsOf'." However, if I directly replace $a, $b, $c with the arguments that I was passing, and then try to run that, it works fine. What the heck is going on? Note: I am using powershell

How to use the Copy-Item cmdlet correctly to copy piped files

人盡茶涼 提交于 2019-12-08 14:58:09
问题 I am building a small script which should copy all .zip files to a special folder called F:\tempzip . I tried it with Copy-Item cmdlet, but I didn't manage to do it. The script should copy all files from this folder (recursively) which are ".zip". This is the part of the script I am talking about: get-childitem F:\Work\xxx\xxx\xxx -recurse ` | where {$_.extension -eq ".zip"} ` | copy-item F:\tempzip What do I have to add? 回答1: When piping items to copy-item you need to tell it that "F:

Windows Command Line FTP to deploy website

百般思念 提交于 2019-12-08 12:55:19
问题 Trying to set up a post build script on my CI server to push changes to our web server by FTP. In as few lines as possible how can i push a folder of files to my webserver using windows FTP? For example deployment folder is: c:\deployment\*.* How can i recursively push all files to replace on the web server? I'm open to using cmd or powershell - MS Windows only Thanks 回答1: Windows' built-in command-line FTP client doesn't have recursion built-in. The easiest way would be to use a different

Listing files in SharePoint library without SharePoint module via Powershell

此生再无相见时 提交于 2019-12-08 11:37:41
问题 We have a powershell script running on our Exchange server (Exchange 2010/Win 2008 R2) which needs to query a SharePoint document library in SharePoint 2013 to find a file whose name can be different every day. For example, Stats_2015_10_14_06_00_01.csv or Stats_2015_10_15_06_05_01.csv This precludes me from being able to hardcode the file name and use the System.Net.WebClient method to download the file locally. I also don't have the SharePoint module installed on the Exchange server and of