NuGet add within Powershell - 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

Related

PowerShell PackageManagement - How to uninstall a package provider?

Troubleshooting i tried uninstalling nuget. NuGetProvider-2.8.5.208.dll to be specific.
I have never worked with Power Shell before and am totally lost.
I found this question that helped me find nuget and create a command to delete it.
But I'm unable to follow through.
I did start powershell as administrator.
Here the input:
(Get-PackageProvider|where-object{$_.name -eq "nuget"}).ProviderPath|Remove-Item -force Restart-Computer
The error message:
Remove-Item : The input object cannot be bound to any parameters of the command because the command does not accept pipeline input or the input and its properties do not match any of the parameters that accept pipeline input.
and its properties do not match any of the parameters that accept pipeline input.
line:1 character:70
+ ... _.name -eq "nuget"}).ProviderPath|Remove-Item -force Restart-Computer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\Users\XXX ...GetProvider.dll:String) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.RemoveItemCommand
How can i solve this and successfully uninstall it?
Thanks for your answer!
Still no luck. Here the error:
Remove-Item : The element C:\Users\XXX\AppData\Local\PackageManagement\ProviderAssemblies\nuget\2.8.5.208
\Microsoft.PackageManagement.NuGetProvider.dll cannot be removed: Access to the path "C:\Users\XXX\AppData\Local\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvide
r.dll" denied.
Line:1 Character:44
+ (Get-PackageProvider NuGet).ProviderPath | Remove-Item -Force
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Users\XXX ...GetProvider.dll:FileInfo) [Remove-Item], Unauthoriz
edAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand

Powershell inside AutohotKey is not working

I can run any PowerShell command inside AHK file. For example, this command below:
Run, PowerShell.exe -noexit -Command "Start notepad; start calc.exe",, hide
I have Powershell command that I can run it from PowerShell fine and its working great which will add user as local admin on remote machine:
([ADSI]"WinNT://serverabc123/Administrators,Group").Add("WinNT://HMM.org.br/Paul1")
However, when I use the above code on AHK like it's not working!
Run, PowerShell.exe -noexit -Command "([ADSI]"WinNT://serverabc123/Administrators,Group").Add("WinNT://HMM.org.br/Paul1")",, hide
I keep getting this error:
At line:1 char:8<>
+ ([ADSI]WinNT://serverabc123/Administrators
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'WinNT://serverabc123/Administrators' in expression or statement.
At line:1 char:8
+ ([ADSI]WinNT://serverabc123/Administrators
+ ~
Missing closing ')' in expression.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
Can anyone advice on what to do? I could write external file PS1 and call it from the AHK then delete it, but I want to have it running from my single AHK file.
Try the following:
Run, powershell.exe -noexit -command ([ADSI]\"WinNT://serverabc123/Administrators`,Group\").Add(\"WinNT://HMM.org.br/Paul1\")
That is:
Don't use outer quoting.
\-escape " characters (required by PowerShell)
`-escape , characters (required by AutoHotkey)

Powershell invoking DTUTIL error

I'm currently in the process of building a deployment process in octopus deploy and as part of that I want to use DTUTIL to create folders and load SSIS packages to our server.
This text "C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\dtutil.exe" /SourceServer [servername] /FC SQL;\;[foldername], with appropriate server and folder names, which works in DOS.
Here is the rub, if I use the same code in Powershell I get an error:
$Command ="`"C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\dtutil.exe`" /SourceServer MYSERVER /FC SQL;\;Folder1";
invoke-Expression $Command;
invoke-expression : At line:1 char:73
+ ... Files (x86)\Microsoft SQL Server\130\DTS\Binn\dtutil.exe" /SourceSe ...
+ ~
You must provide a value expression following the '/' operator.
At line:1 char:73
+ ... \Microsoft SQL Server\130\DTS\Binn\dtutil.exe" /SourceServer MYSERVER ...
+ ~~~~~~~~~~~~
Unexpected token 'SourceServer' in expression or statement.
At line:1 char:1
+ invoke-expression $Command
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : ExpectedValueExpression,Microsoft.PowerShell.Commands.InvokeExpressionCommand
Has anyone got any idea how to remedy this?
Anthony
You can execute the EXE using the & operator:
& "C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\dtutil.exe" /SourceServer MYSERVER /FC SQL;\;Folder1
Also, the above Invoke-Expression command failed in the same way when I ran it in PowerShell directly, this doesn't appear to be an issue with running the PowerShell within Octopus.

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}"

powershell execute appcmd with /IN < pathToFile

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