powershell-2.0

Comparing array variables in PowerShell

瘦欲@ 提交于 2019-11-27 08:40:06
I have this script to compare two folders. $firstfolder = Get-ChildItem C:\firstfolder $secondfolder = Get-ChildItem C:\firstfolder if ($firstfolder -eq $secondfolder) { Write-Host "Folders are the same." } else { Write-Host "Error: Doesn't match." } As you can see, I compare the same folder. Problem is, that it will never consider, that the arrays are equal. Why? Enrico Campidoglio In PowerShell, variables that point to arrays are evaluated in expressions by enumerating the contents of the arrays themselves. For example this expression: $firstFolder | Get-Member will return information about

PowerShell: how to convert a COM object to an .NET interop type?

天大地大妈咪最大 提交于 2019-11-27 07:48:37
问题 As described in my question Create ISO image using PowerShell: how to save IStream to file?, in PowerShell I create an IStream object as follows: $is = (New-Object -ComObject IMAPI2FS.MsftFileSystemImage).CreateResultImage().ImageStream This object is of (PowerShell) type System.__ComObject . And somehow PowerShell knows that it is an IStream : PS C:\> $is -is [System.Runtime.InteropServices.ComTypes.IConnectionPoint] False PS C:\> $is -is [System.Runtime.InteropServices.ComTypes.IStream]

How do I include a locally defined function when using PowerShell's Invoke-Command for remoting?

爷,独闯天下 提交于 2019-11-27 07:26:16
I feel like I'm missing something that should be obvious, but I just can't figure out how to do this. I have a ps1 script that has a function defined in it. It calls the function and then tries using it remotely: function foo { Param([string]$x) Write-Output $x } foo "Hi!" Invoke-Command -ScriptBlock { foo "Bye!" } -ComputerName someserver.example.com -Credential someuser@example.com This short example script prints "Hi!" and then crashes saying "The term 'foo' is not recognized as the name of a cmdlet, function, script file, or operable program." I understand that the function is not defined

copy-item With Alternate Credentials

徘徊边缘 提交于 2019-11-27 07:24:41
I'm using the CTP of powershell v2. I have a script written that needs to go out to various network shares in our dmz and copy some files. However, the issue I have is that evidently powershell's cmdlets such as copy-item, test-path, etc do not support alternate credentials... Anyone have a suggestion on how best to accomplish my task..? chills42 I have encountered this recently, and in the most recent versions of Powershell there is a new BitsTransfer Module , which allows file transfers using BITS , and supports the use of the -Credential parameter. The following sample shows how to use the

Powershell script to see currently logged in users (domain and machine) + status (active, idle, away)

冷暖自知 提交于 2019-11-27 07:05:38
I am searching for a simple command to see logged on users on server. I know this one : Get-WmiObject -Class win32_computersystem but this will not provide me the info I need. It returns : domain Manufactureer Model Name (Machine name) PrimaryOwnerName TotalPhysicalMemory I run Powershell 3.0 on a Windows 2012 server. Also Get-WmiObject Win32_LoggedOnUser -ComputerName $Computer | Select Antecedent -Unique gives me not the exact answers I need. I would love to see as well the idle time, or if they are active or away. Hyon In search of this same solution, I found what I needed under a different

How to keep the shell window open after running a PowerShell script?

一曲冷凌霜 提交于 2019-11-27 07:04:11
I have a very short PowerShell script that connects to a server and imports the AD module. I'd like to run the script simply by double clicking, but I'm afraid the window immediately closes after the last line. How can I sort this out? You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post . One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. e.g. PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1" Per-script Fix: Add a prompt for input to the end of your

List hidden sub-directories and sizes

时光毁灭记忆、已成空白 提交于 2019-11-27 06:59:28
问题 I need to get a list of sub-directories with their sizes using PowerShell. The following PowerShell code does what I want, but it does not work with hidden directories. Get-ChildItem | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.Name + ": " + "{0:N2}" -f ((Get-ChildItem $_ -Recurse | Measure-Object Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) + " MB" } I tried showing hidden directories in Windows, but that does't work. For now, I just make sure every sub-directory is not

How do I concatenate two text files in PowerShell?

我只是一个虾纸丫 提交于 2019-11-27 06:26:16
I am trying to replicate the functionality of the "cat" command in Unix. I would like to avoid solutions where I explicitly read both files into variables, concatenate the variables together, and then write out the concatenated variable. You can simply use cat example1.txt, example2.txt | sc examples.txt . You can surely concatenate more than two files with this style, too. Plus, if the files are named similarly, you can use: cat example*.txt | sc allexamples.txt The cat is an alias for Get-Content , and sc is an alias for Set-Content . Note 1 : Be careful with the latter method - if you try

Web.Config transforms outside of Microsoft MSBuild?

孤街浪徒 提交于 2019-11-27 06:20:37
Is it possible to use Microsoft's XML document transform, for preparing web.configs, outside of MSBuild? I would like to use PowerShell to do these transform without having to run this through the MSBuild engine. If Microsoft had used standard XSLT it would be easy to do in PowerShell. From what I can tell I have to use their C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll which requires a build engine. Thanks Mike DaCosta I created a small function to handle Microsoft's XML Document Transform in PowerShell. I copied the Microsoft.Web

Determine if windows is currently playing sound

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 06:12: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