The first sorry for my english, but I need your help with powershell scripting.
I must config IIS server with powershell script and I have problem with "add application". I need to check whether webapplication exist in site. I want
choose all webapplication in site "testsite2"
$webapplication=Get-WebApplication | Where-Object {$_. applicationpool -eq "testsite2"} | select -expand Name;
But I have problem that command is ending with error
select : Property "Name" cannot be found.At line:1 char:75 Get-WebApplication | Where-Object {$_. applicationpool -eq "testsite2"} | select ... CategoryInfo : InvalidArgument: (Microsoft.IIs.P...gurationElement:PSObject) [Select-Object], PSArgumentException FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
I don´t understand. What have I do when I want choose value from column "Name"?
I get same result
Get-ChildItem –Path IIS:\AppPools\ | Where-Object {$_. Name -eq "testsite2"} | select -ExpandProperty applications
result:
select : Property "applications" cannot be found.
At line:1 char:80
Get-ChildItem –Path IIS:\AppPools\ | Where-Object {$_. Name -eq "testsite2"} | s ...
CategoryInfo : InvalidArgument: (Microsoft.IIs.P...gurationElement:PSObject) [Select-Object], PSArgumentException
FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
The WebAdministration module can be a bit deceiving in that it uses custom formatting data to show stuff like Name with Format-Table (even though the underlying objects have no Name property).
To get the equivalent Name value, take the Path property and trim /:
$WebAppNames = Get-WebApplication |Select #{Name='Name';Expression={$_.Path.Trim('/')}} |Select -Expand Name
The above is exactly what the formatting subsystem in PowerShell does to calculate the Name value.
You can explore the actual properties an object has with the Get-Member cmdlet:
Get-WebApplication |Get-Member
This will also show you the type name, which you can use to explore any additional Type- or Format-Data that might apply:
Get-TypeData -TypeName 'Microsoft.IIS.PowerShell.Framework.ConfigurationElement#site#application'
Get-FormatData -TypeName 'Microsoft.IIS.PowerShell.Framework.ConfigurationElement#site#application'
Related
I'm doing the following powershell line, and for some reason it's finding a syntax error with it, but I'm not sure why, and I can't find it in an internet search:
$eventInitTerminate = Get-WinEvent -FilterHashtable #{LogName='Application';ProviderName='chromoting';StartTime=$initTime;EndTime=$crashOccurredTime;} -ErrorAction SilentlyContinue | Where-Object -PipelineVariable Message -Match 'Channel'
I can see Channel in my chromoting ProviderName for Application event-log, but for some reason it's not working here and I get the error message:
Where-Object : The specified operator requires both the -Property and -Value parameters. Provide values for both parameters, and then try the
command again.
At E:\dirName\CrashAfterExclude_test2.ps1:25 char:192
+ ... tlyContinue | Where-Object -PipelineVariable Message -Match 'Channel'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Where-Object], PSArgumentException
+ FullyQualifiedErrorId : ValueNotSpecifiedForWhereObject,Microsoft.PowerShell.Commands.WhereObjectCommand
I am trying to filter all methods named 'Create' in all workspaces as part of a learning Powershell exercise.
I have this command which seems to list all the methods but I am unable to filter on 'Create' when I pipe the output of teh below to either -Filter or where.
What am I doing wrong here?
Get-WmiObject * -List | Where-Object {$_.methods} | select -ExpandProperty Methods
With -Filter
PS C:\Windows\system32> Get-WmiObject * -List | Where-Object {$_.methods} | select -ExpandProperty Methods | -Filter
-Filter : The term '-Filter' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:86
+ ... Where-Object {$_.methods} | select -ExpandProperty Methods | -Filter
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (-Filter:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Or select where
PS C:\Windows\system32> Get-WmiObject * -List | Where-Object {$_.methods} | select -ExpandProperty Methods | select name
-eq "Create"
Select-Object : A parameter cannot be found that matches parameter name 'eq'.
At line:1 char:98
+ ... _.methods} | select -ExpandProperty Methods | select name -eq "Create ...
+ ~~~
+ CategoryInfo : InvalidArgument: (:) [Select-Object], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
If you want to list the actual Create methods themselves:
Get-WmiObject -List |
Select-Object -ExpandProperty Methods |
Where-Object Name -eq Create
If, by contrast, you want to list the classes that have a Create method:
Get-WmiObject -List | Where-Object { $_.Methods.Name -contains 'Create' }
As for what you tried:
-Filter is a parameter, yet you tried to use it as a command. It's also unclear what you're trying to filter, given that there's no argument.
select name -eq "Create" mistakenly applies the Where-Object syntax to the Select-Object (select) cmdlet.
In both attempts:
the use of Where-Object {$_.methods} is unnecessary, because the
Select-Object -ExpandProperty Methods call will automatically ignore method-less classes.
-List by itself is enough to list all classes; no need for wildcard *.
I am currently trying to run a script that is as follows (to find and uninstall CCleaner):
Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {$_.DisplayName -eq "CCleaner"} -OutVariable Results **{
& "$($Results.InstallLocation)\uninst.exe"
}
The error is:
Where-Object : A positional parameter cannot be found that accepts argument
& "$($Results.InstallLocation)\uninst.exe" /S
.At line:1 char:98
+ Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object <<<< {$_.DisplayName -eq "CCleaner"} -OutVariable Results {
+ CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WhereObjectCommand
The last part there seems to be a problem. I am sure this is because I am writing this in PS v3 but I'm running this as a PSSession on PC's running PS v2 or v1.
I think this is what you're after:
Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object {$_.DisplayName -eq "CCleaner"} | ForEach-Object { & "$($_.InstallLocation)\uninst.exe" }
Otherwise, you need to use a named parameter after "Results"
Can explain why philosophically this doesn't work?
Just as a learning example, I wanted to see the properties of the get-service cmdlet, without the events or methods.
PS C:\Users\Neal> get-service | get-member | {$_.name -eq "Property"}
Result:
At line:1 char:29
+ get-service | get-member | {$_.name -eq "Property"}
+ ~~~~~~~~~~~~~~~~~~~~~~~~
Expressions are only allowed as the first element of a pipeline.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
{$_.name -eq "Property"} is just a scriptblock. If you want to use Where-Object to filter the results of get-member, you need to type Where-Object:
PS C:\Users\Neal> get-service | get-member | Where-Object {$_.name -eq "Property"}
or you can use where, which is an alias for Where-Object:
PS C:\Users\Neal> get-service | get-member | where {$_.name -eq "Property"}
There is even a special character ? which refers to Where-Object:
PS C:\Users\Neal> get-service | get-member | ? {$_.name -eq "Property"}
All three examples given above do the same thing. Choosing between them is simply a matter of style.
I am very new to powershell and am running into a road block I can not figure out. I have a CSV with a list of 380 user accounts with one white space behind their samaccount name. This is on a Windows 2003 server that has PSv2 and Quest.Activeroles.admanagement
This is what I have so far
`add-pssnapin quest.activeroles.admanagement
get-PSSnapin Quest.ActiveRoles.ADManagement
Import-Csv "C:\Documents and Settings\%user%\Desktop\withspaces2.csv" |
ForEach{
Set-QADUser $_.sAMAccountName.Replace(' ',"").Replace("\t","")
}`
It is removing the white space for the first user but doing nothing to the others here is what the Errors looks like.
Name Type DN
---- ---- --
ABPR user CN=ABPR,CN=Users,DC=DRIVERS,DC=...
ADDJ user CN=ADDJ,CN=Users,DC=DRIVERS,DC=...
Set-QADUser : Ambiguous identity: ALLMA.
At line:3 char:12
Set-QADUser <<<< $_.sAMAccountName.Replace(' ',"").Replace("\t","")
CategoryInfo : NotSpecified: (:) [Set-QADUser], IdentityException
FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.BusinessLogi
c.IdentityException,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets
.SetUserCmdlet
ALLR user CN=ALLR,CN=Users,DC=DRIVERS,DC=...
ALLS user CN=ALLS,CN=Users,DC=DRIVERS,DC=...
Set-QADUser : Ambiguous identity: AMAB.
At line:3 char:12
Set-QADUser <<<< $_.sAMAccountName.Replace(' ',"").Replace("\t","")
CategoryInfo : NotSpecified: (:) [Set-QADUser], IdentityException
FullyQualifiedErrorId : Quest.ActiveRoles.ArsPowerShellSnapIn.BusinessLogi
c.IdentityException,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets
.SetUserCmdlet
Try looking into .Trim() rather than replace
Example:
import-csv "mycsv" | foreach-object {get-qaduser -sAMAccountName $_.sAMAccountName | set qaduser -sAMAccountName $_.sAMAccountName.Trim()}
I haven't tested but it's worth a look.
Ignore the formatting, this is just to demonstrate that String.Trim() removes the last blank space from your string and exports it to another CSV to verify.
$array = #()
Foreach ($item in Import-Csv 'C:\CSVFileName')
{
$obj = New-Object PsObject
$obj | Add-Member -Name samAccountName -MemberType NoteProperty -Value $item.samAccountName.Trim() #Value is the name of the column with your samAccountNames
$array += $obj
}
$array | Export-Csv C:\NewCSVFile.Csv -NoTypeInformation