powershell-2.0

How to check if a cmdlet exists in PowerShell at runtime via script

a 夏天 提交于 2019-12-03 08:06:14
问题 I have a PowerShell script that needs to run under multiple hosts (PowerGUI, PowerShell ISE, etc...), but I am having an issue where sometimes a cmdlet doesn't exist under one of the hosts. Is there a way to check to see if a cmdlet exists so that I can wrap the code in an if block and do something else when it does not exist? I know I could use the $host.name to section the code that is suppose to run on each host, but I would prefer to use Feature Detection instead in case the cmdlet ever

How to timeout PowerShell function call

跟風遠走 提交于 2019-12-03 06:52:05
I wrote a little powershell function that executes Get-EventLog against remote servers. On some servers this seems to just hang and never times out. Can I timeout a powershell function call? I see how to do this against a different process , but i want to do this for a power shell function. thanks ####################### function Get-Alert4 { param($computer) $ret = Get-EventLog application -after (get-date).addHours(-2) -computer $computer | select-string -inputobject{$_.message} -pattern "Some Error String" | select-object List return $ret } # You can implement timeouts by using a background

Changing the physical path of an IIS website on a remote machine via Powershell

*爱你&永不变心* 提交于 2019-12-03 06:40:37
问题 I'm currently working on a deployment script that will take my site, export it from svn, remove any testing files etc in it, minify the javascript/css, copy the code to a remote web server, and then switch the physical path of the existing site to the new directory. So far I have everything working except for switching the physical directory in IIS. $IIsServer = Get-WmiObject Site -Namespace "root/WebAdministration" -ComputerName $serverIP -Credential $credentials -Authentication

Can I determine if a PowerShell function is running as part of a pipeline?

試著忘記壹切 提交于 2019-12-03 06:18:37
Can a PowerShell function determine if it is being run as part of a pipeline? I have a function which populates an array with instances of FileInfo which I would like to "yield" to the pipeline if the function is being run this way or produce some pretty output if the function is being invoked by itself from the command line. function Do-Something { $file_infos = @() # Populate $file_infos with FileInfo instances... if (INVOKED_IN_PIPELINE) { return $file_infos } else { foreach ($file_info in $file_infos) { write-host -foregroundcolor yellow $file_info.fullname } } } Basically, I'm trying to

List of all colors available for PowerShell?

与世无争的帅哥 提交于 2019-12-03 05:30:35
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" 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([ConsoleColor]) Pretty grid $colors = [enum]::GetValues([System.ConsoleColor]) Foreach ($bgcolor in $colors){

How to copy folder with subfolders? [duplicate]

瘦欲@ 提交于 2019-12-03 05:22:20
This question already has an answer here: How to use PowerShell copy-item and keep structure 9 answers This script works perfectly in PowerShell. It copies all files with specific type. But I want copy files with it folders & subfolders. $dest = "C:\example" $files = Get-ChildItem -Path "C:\example" -Filter "*.msg" -Recurse foreach ($file in $files) { $file_path = Join-Path -Path $dest -ChildPath $file.Name $i = 1 while (Test-Path -Path $file_path) { $i++ $file_path = Join-Path -Path $dest -ChildPath "$($file.BaseName)_$($i)$($file.Extension)" } Copy-Item -Path $file.FullName -Destination

GetType used in PowerShell, difference between variables

断了今生、忘了曾经 提交于 2019-12-03 04:41:03
问题 What is the difference between variables $a and $b ? $a = (Get-Date).DayOfWeek $b = Get-Date | Select-Object DayOfWeek I tried to check $a.GetType $b.GetType MemberType : Method OverloadDefinitions : {type GetType()} TypeNameOfValue : System.Management.Automation.PSMethod Value : type GetType() Name : GetType IsInstance : True MemberType : Method OverloadDefinitions : {type GetType()} TypeNameOfValue : System.Management.Automation.PSMethod Value : type GetType() Name : GetType IsInstance :

Copy-item Files in Folders and subfolders in the same directory structure of source server using PowerShell

大憨熊 提交于 2019-12-03 01:12:37
I am struggling really hard to get this below script worked to copy the files in folders and sub folders in the proper structure (As the source server). Lets say, there are folders mentioned below: Main Folder: File aaa, File bbb Sub Folder a: File 1, File 2, File 3 Sub Folder b: File 4, File 5, File 6 Script used: Get-ChildItem -Path \\Server1\Test -recurse | ForEach-Object { Copy-Item -LiteralPath $_.FullName -Destination \\server2\test | Get-Acl -Path $_.FullName | Set-Acl -Path "\\server2\test\$(Split-Path -Path $_.FullName -Leaf)" } Output: File aaa, File bbb Sub Folder a (Empty Folder)

Compiling and running java application using powershell

梦想的初衷 提交于 2019-12-03 00:43:31
I am trying to compile and a sample Helloworld.java file. I have my jdk installed in C:\Program Files\jdk1.7\bin. And I have my Helloworld.java in C:\Helloworld.java I am actually a novice in both powershell and java. I got some examples from web regarding this but many of them advice to run it like this: java.exe -classpath $Env:CLASSPATH C:\Helloworld.java But when I give this in powershell I get an error called 'CLASSPATH' is not defined even after adding it in env variables. And when I try to compile the code with the following syntax: $javac C:\Helloworld.java I get an error "javac is not

How to embed Images in a powershell email using MailMessage

我只是一个虾纸丫 提交于 2019-12-02 23:08:22
问题 I have an email that works from PS. What I have been trying to do is include images embedded in the email (not attachments). Below is what I have so far: function Email { $smtpServer = {smtp server} $smtpFrom = {email from} $smtpTo = {email to} $messageSubject = "test" $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto $message.Subject = $messageSubject $message.IsBodyHTML = $true $credentials=new-object system.net.networkcredential({smtpUsername},{smtpPassword}) # $message