Quantcast
Channel: PowerShell.org » All Posts
Viewing all 13067 articles
Browse latest View live

Reply To: Handling a million quotes in a string

$
0
0

Try a herestring:

$website = "https://somesite/v11/host/test/Joe"
$address = "127.0.0.1"
$notes= "this is a test"
 
$hereString = @"
plink -load session curl -vvvv -XPUT –negotiate -u : –insecure -H "Content-Type: application/json" -d '{"hosttype":"server", "address": ["$address"], "parents": [], "tag": ["demo"], "umiworker": "nowwhere", "notes": "$notes", "url": "http:/www.google.com"}' $website
"@
 
&$hereString

Reply To: Handling a million quotes in a string

Reply To: Invoking Registry UninstallString

$
0
0

Previously, I've set the location prior to script execution with the "Set-Location" cmdlet. I've since moved the set-location to line 13.
The script processes, but still $uninst variable is not performing as if I run "Silverlight.Configuration.exe -uninstallApp *sloobapp*" straight at the command line.

Now I believe this is happening because on line 11 i'm setting "$uninst = ($aps.UninstallString)" which is the registry uninstall string pulled from the key above. I'm attempting to run it in this variable.

(pre){{###Sets variable for reg HKCU query, filters for SLOOBAPP, displays displayname, uninstallstring properties
$apsInst = Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall|
Get-ItemProperty|Where-Object {$_.DisplayName -match "SLOOBAPP"}
Select-Object -Property DisplayName, UninstallString

ForEach ($aps in $apsInst) {

If ($aps.UninstallString) {

$uninst = $aps.UninstallString

Set-Location -Path "c:\Program Files (x86)\Microsoft Silverlight\5.1.30514.0"

Start-Process -FilePath cmd.exe -ArgumentList '/c', $uninst -wait
}

}} (/pre)

Reply To: Slack for Powershell

Reply To: Piping computers to Get-Service

$
0
0

The default pipeline variable in Get-Service is -Name, meaning you can't pipe a ComputerName into Get-Service. You pipe a service name, like

'wuauserv' | Get-Service
 
Status   Name               DisplayName
——   —-               ———–
Stopped  wuauserv           Windows Update

What you can do is flip it on its head, and do

Get-Service -ComputerName (Get-ADComputer -Filter { Name -like 'HYPERV*' } | Select-Object -ExpandProperty Name)

But it's not very pretty since Get-Service doesn't return the ComputerName, and Get-Service with -ComputerName uses the oldschool RPC protocol to query for data thanks to it being one of the very first commands introduced to PowerShell and never having been updated since. Instead I'd do this

Invoke-Command -ComputerName (Get-ADComputer -Filter { Name -like 'HYPERV*' } | Select-Object -ExpandProperty Name) -ScriptBlock { Get-Service }

Reply To: Piping computers to Get-Service

$
0
0

Just for giggles, what happens if you do this?

Get-ADComputer -filter * | Select-Object @{Name="ComputerName"; Expression={ [string]$_.Name } | Get-Service

Parameter binding tries to do things without coercion first (where the piped object's value or property exactly matches the type of the parameters), and only then falls through to trying type coercion. From your error message, it looks like you're making it all the way to the ByValue (With Coercion) binding, so maybe forcing your ComputerName property to be a String object will help.

Also, the AD cmdlets are a pain. Their output objects behave very strangely, which sometimes leads to these types of problems in the pipeline. Whoever wrote that module was way too clever for their own good, and it leads to all sorts of gotchas. (Did you know that every single parameter in the AD cmdlets is dynamic? Why? Beats the heck out of me, but try creating a proxy function for one sometime, and marvel at the empty param() block.)

Reply To: Handling a million quotes in a string

$
0
0

In v3 or later, just use the "stop parsing" operator, –%:

plink –% -load session # etc

Reply To: Handling a million quotes in a string

$
0
0

Bah, our forum software is converting two hyphens into that goofy dash character. The operator is hyphen-hyphen-percent, and anything after it on the line is sent directly to the console application without PS doing any parsing. (This means PowerShell variables after the operator are also not expanded, so be aware of that.)


Reply To: Handling a million quotes in a string

$
0
0

Oh yeah, I always forget about that one.

Reply To: Class-Based DSC Resource ProviderImportFailure WMF 5 Preview April 2015

$
0
0

I know a great many people have gotten class-based resources working. Let me ping a couple of folks to see if they have time to take a look.

However, keep in mind that v5 **is** definitely very much in preview still, and not 100% working. If your goal is to get something working outside of a let's-play-with-this lab, you should indeed switch back to the function-style resources and use them in v4. WMF5 definitely has some bugs and quirks in the current public preview.

Don Jones
Curriculum Director for IT Pro Content, Pluralsight.com

Reply To: Piping computers to Get-Service

$
0
0

Just for giggles, what happens if you do this?

Two things happened for me

PS C:\Users\mni> Get-ADComputer -Filter { Name -like 'HYPERV*' } | Select-Object @{Name="ComputerName"; Expression={ [string]$_.Name } } | Get-Service
Get-Service : Cannot find any service with service name '@{ComputerName=HYPERV2}'.
At line:1 char:123
+ … ]$_.Name } } | Get-Service
+                    ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (@{ComputerName=HYPERV2}:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand

Secondly, the command made my SCVMM console commit suicide. Not entirely sure why.

Reply To: Class-Based DSC Resource ProviderImportFailure WMF 5 Preview April 2015

Reply To: Piping computers to Get-Service

$
0
0

OK, one more attempt to make the AD cmdlet behave:

Get-ADComputer -filter * | Select-Object * | Select-Object @{Name="ComputerName"; Expression={ [string]$_.Name } | Get-Service

Piping to Select-Object * looks ridiculous, but it's one of the common workarounds to make the AD cmdlets work well in a pipeline.

Reply To: Permission Issue – Invoke Command

$
0
0

Will,

Thank you for responding.
When I start the setup.exe directly UAC pops up.
It is the UAC window I'm afraid is cancelling the process.
I will check the event logs.

Any suggestions on handling the UAC?

##Nick

Reply To: Piping computers to Get-Service

$
0
0

Still no dice, at least in my case. I'm pretty sure Get-Service won't take anything but a service name or a service object. Heck, even an explicit [pscustomobject]@{ ComputerName = 'HYPERV01' } doesn't work.

This does however:

PS C:\Users\mni> $service = Get-Service -ComputerName 'HYPERV01' -Name 'wuauserv'
PS C:\Users\mni> $service
 
Status   Name               DisplayName
——   —-               ———–
Stopped  wuauserv           Windows Update
 
 
PS C:\Users\mni> $service | Get-Service
 
Status   Name               DisplayName
——   —-               ———–
Stopped  wuauserv           Windows Update

Reply To: Piping computers to Get-Service

$
0
0

@Dave – Tried the cast to string, still doesn't work. I can do the same thing to Get-Process and it works.

@Martin – Your just creating a string array in your example, but don't understand why you would not be able to pipe to the object. When I pipe to Get-Process, it works. If I look at it's properties, it doesn't have Name as ByValue, but there is InputObject setup for ByValue. Also, your examples are missing a end paren in the ComputerName examples.

I think it's an interesting question. I reproduced the behavior in the function above, so I would be curious on how it would be handled. If it's not an accurate portrayal of how the cmdlet would function, I'd be curious to know what is wrong with the implementation in the function above.

PS C:\> Get-ADComputer MyComputer | Select @{Label="ComputerName";Expression={[string]$_.NAME}} | Get-Service
 
Get-Service : Cannot find any service with service name '@{ComputerName=MyComputer}'.
At line:1 char:94
+ … ng]$_.NAME}} | Get-Service
+                    ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (@{ComputerName=MyComputer}:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
 
 
PS C:\> Get-ADComputer MyComputer | Select @{Label="ComputerName";Expression={[string]$_.NAME}} | Get-Process
 
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                                                                                       
——-  ——    —–      —– —–   ——     — ———–                                                                                                                                       
    259      35     5160       6352   100            2620 agent                                                                                                                                             
     81       7     1164       4204    44            1164 armsvc                                                                                                                                            
    523      16    12448      16304    81   636.47   7452 audiodg  ….

Reply To: Piping computers to Get-Service

$
0
0

Okay, just played around with this a bit on my system. This fails:

[pscustomobject]@{ComputerName = 'localhost'} | Get-Service

It fails because the input object winds up binding to both ComputerName AND Name, which is annoying. This, on the other hand, works fine:

[pscustomobject]@{ComputerName = 'localhost'} | Get-Service -Name *

By binding the -Name parameter to * on the command line, we prevent the pipeline object from giving that parameter junk data. I'm not sure how much of the previous attempts you'll need to keep with regards to the objects coming out of the AD module, but in combination with adding -Name *, you should be able to get it working.

Reply To: Piping computers to Get-Service

$
0
0

Well as I said, Get-Service was one of, if not the first command in PowerShell, and it hasn't been touched since it was introduced. It's very likely that it's just poorly written by todays standards.

Edit: Oh interesting. Maybe I'm wrong and the command is just funky?

Reply To: Piping computers to Get-Service

$
0
0

@Dave – Is there anything wrong in the function above? Should ByValue be removed? I see the same behavior and am just curious if it's Powershell behavior or bad parameter setup. Where would you check and override if the "Name" parameter is NULL and reset the global value?

Reply To: Permission Issue – Invoke Command

$
0
0

SOLVED
Just to document my findings, I was able to determine the reason installation was failing.
I ran the installation on the target machine directly using Windows command prompt.
While my PowerShell script wasn't producing errors, the errors were visible in the Windows command prompt.
My argument list (used with my setup.exe) pointed to a configurationfile that had an incompatible set of switches used which caused the cancelled installation.

Thank you Will for pointing me in the right direction, very often its the direction received that helps find the solution.

##Nick

Viewing all 13067 articles
Browse latest View live