powershell execute appcmd with /IN < pathToFile - powershell

I in the process of building a ps1 file to create all my IIS site(s), Virtual Directories and app pools via appcmd. I have used the appcmd list /xml to get the settings from the master machine and saved them into config files. What I am trying to get powershell to execute an appcmd command
appcmd add apppool /IN < pathtoFile.xml
my issue is within powershell is I can not use the less than symbol:
PS C:\temp\deployments> .\createIISSetup.ps1
The '<' operator is reserved for future use.
At C:\temp\deployments\createIISSetup.ps1:36 char:28
+ .$appcmd add apppool /IN < <<<< $pathToAppPoolSettings;
+ CategoryInfo : ParserError: (<:OperatorToken) [], ParseException
+ FullyQualifiedErrorId : RedirectionNotSupported
if I use the pipe:
appcmd add apppool /IN | pathtoFile.xml
I get:
PS C:\temp\deployments> .\createIISSetup.ps1
Expressions are only allowed as the first element of a pipeline.
At C:\temp\deployments\createIISSetup.ps1:36 char:51
+ .$appcmd add apppool /IN | $pathToAppPoolSettings <<<< ;
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

A colleague gave me the answer for this. Just spun it on its head:
type $pathToapppoolSettings | .$appcmd add apppool/IN;
this works great.

Get-Content appppols.xml | .\appcmd.exe add apppool /in

Related

Installing CCM client on PC via powershell

I'm trying to speed up how soon SCCM recognises a device on the domain and starts the process of installing applications.
I have the below in powershell
$CCMInstallerPath = "\\ServerFQDN\SMS_MAN\Client\ccmsetup.exe"
$args = "/mp:MPServer", "/LOGON", "/UsePKICert", "SMSSLP=SMSSLPServer", "SMSSITECODE=MAN"
& $CCMInstallerPath $args
I get the below error:
Program 'ccmsetup.exe' failed to run: Access is deniedAt line:1 char:5
+ & $CCMInstallerPath $args
+ ~~~~~~~~~~~~~~~~~~~~~~~~~.
At line:1 char:5
+ & $CCMInstallerPath $args
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorId : NativeCommandFailed
I have read and execute access to this file, both by group and directly by name
I have also tried:
Start-Process -FilePath \\ServerFQDN\SMS_MAN\Client\ccmsetup.exe -ArgumentList "/mp:MPServer", "/logon SMSSITECODE-MAN", "/UsePKICert"
and get the error
Start-Process : This command cannot be run due to the error: Access is denied.
At line:1 char:5
+ Start-Process -FilePath \\ServerFQDN\SMS_MAN\Client\c ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
ServerFQDN is a replacement for one of our NM servers
I can fully connect to the folder and run the exe manually
I am running elevated powershell
Issue was caused by incorrect share permissions found in effective access.
Permission was granted to run the file but effective access was showing differences

NuGet add within Powershell

I'm trying to execute the following NuGet add command via a PowerShell script:
Invoke-Expression "'C:\Program Files\NuGet\nuget.exe' add '\packageName.1.1.0.nupkg' -Source '\\file-server\My Nuget Packages'"
However, I keep running into the following errors:
Invoke-Expression : At line:1 char:36
+ 'C:\Program Files\NuGet\nuget.exe' add '\packageName.1.1.0.nupkg' -Source '\\fil ...
+ ~~~
Unexpected token 'add' in expression or statement.
At line:3 char:1
+ Invoke-Expression "'C:\Program Files\NuGet\nuget.exe' add '\packageName.1.1.0.nu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.InvokeExpressionCommand
Is there a way to call the Nuget add script from within Powershell? If so, how would I do so?
Replace Invoke-Expression with & and remove the double quotes surrounding command line, and change single quotes to double quotes:
& "C:\Program Files\NuGet\nuget.exe" add "\packageName.1.1.0.nupkg" -Source "\\file-server\My Nuget Packages"
Although I suspect that \packageName.1.1.0.nupkg should really be .\packageName.1.1.0.nupkg

Powershell not always running ScriptsToProcess on import

After using Install-Module to install powershell-yaml I had about a week of use before I came in one morning to it not loading YamlDotNet properly.
Now if I try to use ConvertFrom-Yaml before manually running Import-Module powershell-yaml I get the the following error:
PS C:\Users\user> "---" | ConvertFrom-Yaml
New-Object : Cannot find type [YamlDotNet.RepresentationModel.YamlStream]: verify that the assembly containing this type is loaded.
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:24 char:23
+ ... $yamlStream = New-Object "YamlDotNet.RepresentationModel.YamlStream"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:25 char:9
+ $yamlStream.Load([System.IO.TextReader] $stringReader)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Previously (and on most systems) this worked fine. If I run a manual Import-Module before calling the function it works fine:
PS C:\Users\user> Import-Module powershell-yaml
PS C:\Users\user> "---" | ConvertFrom-Yaml
PS C:\Users\user>
Both my and a coworker's machines started this behavior at about the same time. This morning my system started working properly again, but his is still exhibiting the behavior. We haven't been able to duplicate it on other machines.
I've narrowed it down to a script referenced in Powershell-Yaml manifest's ScriptsToProcess not being called during autoload, but runs fine during a manual Import-Module. On working machines the script in ScriptsToProcess is run in both cases. As a workaround we can force a module load by putting an Import-Module in our profile, ideally we'd like to find a root cause.
Import-Module -Verbose doesn't help, because calling Import-Module first always works.

How to start powershell in eclipse

I try to start powershell as an external tool.
I want it to be run in the console windows of eclipse.
But it starts with empty console, and when I type and press return, I get:
Missing expression after unary opearator ‘-’.
At line:1 char:2
+ – <<<< Command Set-Location C:\Users\XXX
+ CategoryInfo : ParserError: (-:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingExpressionAfterOperator
You got misplaced quotes in the Arguments section.
-NoExit -"Command Set-Location ${project_loc}"
Try moving the quote after Command switch like so,
-NoExit -Command "Set-Location ${project_loc}"

Executing an EXE in your $PATH, which starts with a numeric value, in Powershell?

So 7z.exe is in my $PATH environment variable.
PS C:\Users\jimmeh> 7z.exe
Bad numeric constant: 7.
At line:1 char:2
+ 7 <<<< z.exe
+ CategoryInfo : ParserError: (7:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : BadNumericConstant
Besides renaming the EXE, is there a way to do this, or is it a bug in Powershell?
It work like
& '.\7z.exe'
if you want to specify an archive to extract
& '.\7z.exe' e your.zip
This is no longer a problem with Powershell 4.0
http://www.microsoft.com/en-us/download/details.aspx?id=40855