powershell-2.0

try-catch-fail with powershell and schtasks

那年仲夏 提交于 2019-11-30 20:35:47
I'm new to powershell, but I'm trying to output some simple logging in a ps I'm writing to create scheduled tasks. My code is below. It seems that it doesn't throw an exception when you get an error with schtasks. Another SO question mentioned this with fileIO actions and suggested doing "-ea stop" but that doesn't work with schtasks. #create log file $log = "\\servername\!incoming\Deploy\log.txt" Clear-Content $log #get input file list $txtServerList = Gc "\\servername\!incoming\Deploy\serverlist.txt" #loop through each server ForEach($strServername In $txtServerList) { try { #install task

Determine if windows is currently playing sound

泪湿孤枕 提交于 2019-11-30 20:05:51
So I've been pondering on this problem for a while and I can't figure out what the right way to go about this is. I want to determine if Windows is outputting sound at a certain time using a Powershell script. I can determine whether or not the audio driver has an error, but I cannot for the life of me figure out if the system is playing sound. I looked at the .NET class for System.Media and the three classes inside all had to do with playing sound or manipulating the system sounds. I'm not asking for code to be written for me, I just need to know where to start to check if the windows system

Passing arguments to Start-Job scriptblock?

瘦欲@ 提交于 2019-11-30 17:57:42
问题 I'd like to setup a cmdlet to start and stop mysql, and I'm trying to do so with Start-Job. the I've got the following in my Powershell profile: $mysqlpath = "C:\Program Files\MySQL\MySQL Server 5.5\bin" Function Start-Mysql { Start-Job -ScriptBlock { & "$mysqlpath\mysqld.exe" } } The variable doesn't seem to be expanding in the job command however? I must be missing some sort of scoping rule. Could someone please advise? Thanks! 回答1: you have to use the -argumentlist parameter see get-help

How to find PowerShell Static Classes and Methods?

喜夏-厌秋 提交于 2019-11-30 16:48:41
How can I find what Static Classes and Methods there is available in PowerShell 2.0? You can use any .NET types and their static methods from PowerShell. To enumerate all that are currently loaded into your AppDomain, you can do: [AppDomain]::CurrentDomain.GetAssemblies() | foreach { $_.GetTypes() } | foreach { $_.GetMethods() } | where { $_.IsStatic } | select DeclaringType, Name | format-table Remember, you are not limited to static methods, you can also instantiate the types using new-object and call instance methods. You can use get-member on an instance to get the methods on a type. Also,

Get relative path of files in sub-folders from the current directory

非 Y 不嫁゛ 提交于 2019-11-30 16:47:12
Is there any straight-forward way (by use of cmdlets or .NET classes) to obtain only the relative path of a file in a sub-folder from a given path? eg current folder is C:\MyScript and there is a sub-folder called Data with a file Test.txt , so I would like to see Data\Test.txt instead of C:\MyScript\Data\Test.txt The Resolve-Path cmdlet has a -Relative parameter that will return a path relative to the current directory: Set-Location C:\MyScript $relativePath = Get-Item Data\Test.txt | Resolve-Path -Relative 来源: https://stackoverflow.com/questions/10972589/get-relative-path-of-files-in-sub

Powershell replacing periods

久未见 提交于 2019-11-30 16:44:22
Can anyone tell me if they think there is something wrong with this Powershell script. Dir | where {$_.extension -eq ".txt"} | Rename-Item –NewName { $_.name –replace “.“,”-” } For each text file in the current directory, I'm trying to replace all periods in the file names with hyphens. Thanks ahead of time. As the others have said, -replace uses regex (and "." is a special character in regex). However, their solutions are forgetting about the fileextension and they are acutally removing it. ex. "test.txt" becomes "test-txt" (no extension). A better solution would be: dir -Filter *.txt |

How to find PowerShell Static Classes and Methods?

梦想的初衷 提交于 2019-11-30 16:26:14
问题 How can I find what Static Classes and Methods there is available in PowerShell 2.0? 回答1: You can use any .NET types and their static methods from PowerShell. To enumerate all that are currently loaded into your AppDomain, you can do: [AppDomain]::CurrentDomain.GetAssemblies() | foreach { $_.GetTypes() } | foreach { $_.GetMethods() } | where { $_.IsStatic } | select DeclaringType, Name | format-table Remember, you are not limited to static methods, you can also instantiate the types using new

Get relative path of files in sub-folders from the current directory

梦想的初衷 提交于 2019-11-30 16:21:46
问题 Is there any straight-forward way (by use of cmdlets or .NET classes) to obtain only the relative path of a file in a sub-folder from a given path? eg current folder is C:\MyScript and there is a sub-folder called Data with a file Test.txt , so I would like to see Data\Test.txt instead of C:\MyScript\Data\Test.txt 回答1: The Resolve-Path cmdlet has a -Relative parameter that will return a path relative to the current directory: Set-Location C:\MyScript $relativePath = Get-Item Data\Test.txt |

How can I get PowerShell Added-Types to use Added Types

霸气de小男生 提交于 2019-11-30 15:24:14
I'm working on a PoSh project that generates CSharp code, and then Add-Type s it into memory. The new types use existing types in an on disk DLL, which is loaded via Add-Type. All is well and good untill I actualy try to invoke methods on the new types. Here's an example of what I'm doing: $PWD = "." rm -Force $PWD\TestClassOne* $code = " namespace TEST{ public class TestClassOne { public int DoNothing() { return 1; } } }" $code | Out-File tcone.cs Add-Type -OutputAssembly $PWD\TestClassOne.dll -OutputType Library -Path $PWD\tcone.cs Add-Type -Path $PWD\TestClassOne.dll $a = New-Object TEST

powershell 2.0 redirection file handle exception

一个人想着一个人 提交于 2019-11-30 15:13:30
问题 I'm looking for a solution to the The OS handle's position is not what FileStream expected. Do not use a handle simultaneously in one FileStream and in Win32 code or another FileStream. exception that would also work on scripts called within the script containing "the fix". For the purposes of this question, say that I have two scripts: foo.ps1 # <fix> $bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField" $objectRef = $host.GetType().GetField( "externalHostRef",