powershell-3.0

How to specify application pool identity user and password from PowerShell

家住魔仙堡 提交于 2019-12-03 09:34:15
I have been having lots of difficulty automating the setup of a Web application and configuring IIS appropriately with the Application Pool Identity. I am doing this in a Web application deployment script written in PowerShell. My requirement is that I need my PowerShell script to set the application pool identity user to a specific service account mydomain\svcuser and password. Here is the sample code: $pool = New-Item "IIS:\AppPools\MyAppPool" -Force $svcuser = "mydomain\svcuser" $pool.processModel.userName = $svcuser $password = "somepassword" $pool.processModel.password = $password $pool

How do I call a parameterless generic method from Powershell v3?

故事扮演 提交于 2019-12-03 05:46:04
For example I have a .NET object $m with the following method overloads: PS C:\Users\Me> $m.GetBody OverloadDefinitions ------------------- T GetBody[T]() T GetBody[T](System.Runtime.Serialization.XmlObjectSerializer serializer) If I try to invoke the parameterless method I get: PS C:\Users\Me> $m.GetBody() Cannot find an overload for "GetBody" and the argument count: "0". At line:1 char:1 + $m.GetBody() + ~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest I understand PowerShell v3.0 is supposed to work more easily with

PowerShell - Setting $ErrorActionPreference for the entire script

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 05:39:32
I've been giving PowerShell (v3.0) a shot for the first time today, and became enormously frustrated with the strange way some of its error-handling concepts are implemented. I wrote the following piece of code (using the Remote Registry PowerShell Module) try { New-RegKey -ComputerName $PCName -Key $Key -Name $Value Write-Host -fore Green ($Key + ": created") } catch { Write-Host -fore Red "Unable to create RegKey: " $Key Write-Host -fore Red $_ } (This is just a snippet) Apparently the default behavior of PowerShell is to NOT catch errors which are non-terminating. So I added the following

Powershell get ipv4 address into a variable

故事扮演 提交于 2019-12-03 05:38:09
问题 Is there an easy way in powershell 3.0 Windows 7 to get the local computer's ipv4 address into a variable? 回答1: Here is another solution: $env:HostIP = ( Get-NetIPConfiguration | Where-Object { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne "Disconnected" } ).IPv4Address.IPAddress 回答2: How about this? (not my real IP Address!) PS C:\> $ipV4 = Test-Connection -ComputerName (hostname) -Count 1 | Select IPV4Address PS C:\> $ipV4 IPV4Address ----------- 192.0.2.0 Note that using

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

Example showing how to override TabExpansion2 in Windows PowerShell 3.0

大憨熊 提交于 2019-12-03 04:26:50
问题 Does anyone have an example showing how to override the TabExpansion2 function in Windows PowerShell 3.0? I know how to override the old TabExpansion function, but I want to provide a list of items for the intellisense in PowerShell ISE. I looked at the definition of TabExpansion2 and it wasn't easily understandable how I inject my own code in the the tab expansion process. 回答1: I think this example should give you a good starting point: Windows Powershell Cookbook: Sample implementation of

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)

Remove a Member from a PowerShell Object?

社会主义新天地 提交于 2019-12-03 00:59:01
I need to remove a member (specifically, a NoteProperty) from an object. How do I accomplish this? Select-Object with ExcludeProperty is good for removing a property from a collection of objects. For removing a property from a single object this method might be more effective: # new object with properties Test and Foo $obj = New-Object -TypeName PSObject -Property @{ Test = 1; Foo = 2 } # remove a property from PSObject.Properties $obj.PSObject.Properties.Remove('Foo') I don't think you can remove from an existing object but you can create a filtered one. $obj = New-Object -TypeName PsObject

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