powershell-2.0

PowerShell Remoting giving “Access is Denied” error

戏子无情 提交于 2019-11-30 06:38:13
I am trying to use PowerShell Remoting to check some disk sizes from a Server in a remote domain, but the commands I am running are failing to work. The situation is like this: Source Server is in Domain A Destination Server is in Domain B There is no trust in place between these domains The Server in Domain B is running Exchange 2010, and I can run Exchange 2010 Specific commands against it from Server A using this command: $Session = New-PSSession -ConfigurationName Microsoft.Exchange –ConnectionUri $ConnectionURI -Credential $Credentials -Authentication Basic Import-PSSession $Session The

PowerShell: detecting errors in script functions

不羁岁月 提交于 2019-11-30 05:22:19
What is the best way to detect if an error occurs in a script function? I'm looking for a consistent way to indicate error/success status similar to $? (which only works on cmdlets, not script functions). Given a particular function might return a value to be used by the caller, we can't indicate success by returning a boolean. A function could use a [ref] parameter and set the value appropriately inside the function and check after the call, but this is more overhead than I'd like. Is there something built-in to PowerShell that we can use? The best I can come up with is: In the function use

Auto Complete User Input PowerShell 2.0

落花浮王杯 提交于 2019-11-30 04:22:05
问题 I have a large list of data (over 1000 different values) and I want the user to be able to select certain values from the list from a PowerShell console. What is the easiest way from within the console to allow the user to quickly select values? I would like to do something like tab completion or the ability to use the arrow keys to scroll through the values but I am not sure how to do either of these things. Any advice would be greatly appreciated. 回答1: PowerShell tab completion can be

try-catch-fail with powershell and schtasks

冷暖自知 提交于 2019-11-30 04:03:36
问题 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

Setup default date format like yyyy-mm-dd in Powershell?

天涯浪子 提交于 2019-11-30 03:44:58
问题 A simple & short question: How can I setup a default date format in powershell like yyyy-mm-dd ? so any date output will be like this format? or How to setup a date format globally in one script ? Is there a way to output date only without time? when I output LastWriteTime, Default is 13-03-2014 14:51 I only need 13-03-2014 but 14:51 . 回答1: A date in PowerShell is a DateTime object. If you want a date string in a particular format, just use the built-in string formatting. PS C:\> $date = get

How can I extract “Path to executable” of all services with PowerShell

邮差的信 提交于 2019-11-30 03:10:58
Get-Service *sql* | sort DisplayName | out-file c:/servicelist.txt I have a one line PowerShell script to extract list of all services running on my local machine, now, in addition to displaying "Status", "Name" and "DisplayName" I also want to display "Path to executable" I think you'll need to resort to WMI: Get-WmiObject win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, State, PathName Update If you want to perform some manipulation on the selected data, you can use calculated properties as described here . For example if you just wanted the text within quotes for the

How to run a Powershell script from the command line and pass a directory as a parameter

好久不见. 提交于 2019-11-30 01:12:24
My Powershell script, Foo.ps1: Function Foo($directory) { echo $directory } if ($args.Length -eq 0) { echo "Usage: Foo <directory>" } else { Foo($args[0]) } From the Windows console: powershell -command .\Foo.ps1 Results in: "The term '.\Foo.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again." This is despite Foo.ps1 being in the current directory from where I am calling Powershell. I then tried to call it specifying the full path to the script file;

Powershell replacing periods

岁酱吖の 提交于 2019-11-30 00:03:21
问题 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. 回答1: 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

Run my third-party DLL file with PowerShell

亡梦爱人 提交于 2019-11-29 23:01:50
I am not sure if this is possible or not with PowerShell. But basically I have a Windows Forms program that configures a program called EO Server. The EO Server has an API, and I make a reference to EOServerAPI.dll to make the following code run. using EOserverAPI; ... private void myButton_Click(object sender, EventArgs e) { String MDSConnString="Data Source=MSI;Initial Catalog=EOMDS;Integrated Security=True;"; //Create the connection IEOMDSAPI myEOMDSAPI = EOMDSAPI.Create(MDSConnString); //Get JobID Guid myMasterJobID = myEOMDSAPI.GetJobID("myJobRocks"); } Is it possible to interact with an

powershell - extract file name and extension

情到浓时终转凉″ 提交于 2019-11-29 22:44:17
I need to extract file name and extension from e.g. my.file.xlsx. I don't know the name of file or extension and there may be more dots in the name, so I need to search the string from the right and when I find first dot (or last from the left), extract the part on the right side and the part on the left side from that dot. Maybe there is better solution, but I did'n find anything here or anywhere else. Thank you If the file is coming off the disk and as others have stated, use the BaseName and Extension properties: PS C:\> dir *.xlsx | select BaseName,Extension BaseName Extension -------- ---