I am removing some IP addresses by using:
Remove-NetIPAddress -InterfaceIndex $my.InterfaceIndex.
Which works OK. But when I add the parameter -Confirm
Remove-NetIPAddress -InterfaceIndex $my.InterfaceIndex -Confirm $false.
It fails as follows:
Remove-NetIPAddress : No matching MSFT_NetIPAddress objects found by CIM query for instances of the ROOT/StandardCimv2/MSFT_NetIPAddress
class on the CIM server: SELECT * FROM MSFT_NetIPAddress WHERE ((IPAddress LIKE 'False')) AND ((InterfaceIndex = 15)). Verify query
parameters and retry.
At line:9 char:1
+ Remove-NetIPAddress -InterfaceIndex $my.InterfaceIndex -Confirm $false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (MSFT_NetIPAddress:String) [Remove-NetIPAddress], CimJobException
+ FullyQualifiedErrorId : CmdletizationQuery_NotFound,Remove-NetIPAddress
How should I use the -Confirm, and -PolicyStore parameters?
Because -Confirm is a Switch parameter you pass false to it with a colon:
Remove-NetIPAddress -InterfaceIndex $my.InterfaceIndex -Confirm:$false
You pass true to it by just declaring it on its own.
Related
After looking at various stackoverflow questions, I found several ways to download a file from a command line without interaction from the user.
The only one that worked for me also works only on Windows 10 natively :
curl -sko %TEMP%\file.txt "https://some.hostname/file.txt"
But installing an external tool like wget/curl is what I want to avoid.
What didn't work for me because of proxy errors :
Command:
bitsadmin.exe /transfer "dljob" "https://some.hostname/file.txt" %TEMP%\file.txt
Error:
DISPLAY: 'dljob' TYPE: DOWNLOAD STATE: ERROR
PRIORITY: NORMAL FILES: 0 / 1 BYTES: 0 / UNKNOWN
Unable to complete transfer.
ERROR FILE: https://some.hostname/file.txt -> E:\Users\xxx\AppData\Local\Temp\file.txt
ERROR CODE: 0x80190197
ERROR CONTEXT: 0x00000005
Command:
powershell -Command "(New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt', '%TEMP%\file.txt')"
Error:
Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (407) Proxy Authentication Required."
At line:1 char:1
+ (New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Command:
powershell -Command "Invoke-WebRequest 'https://some.hostname/file.txt' -OutFile %TEMP%\file.txt
Error:
Invoke-WebRequest :
Authentication required
You must be authenticated to access this URL.
...
Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.17763.1007
At line:1 char:1
+ Invoke-WebRequest 'https://some.hostname/file.txt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
This didn't work either :
powershell -Command "$client.Credentials = Get-Credential; $browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials; (New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt', 'file.txt')"
Error :
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Get-Credential : Cannot process command because of one or more missing mandatory parameters: Credential.
At line:1 char:23
+ $client.Credentials = Get-Credential; $browser.Proxy.Credentials =[Sy ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Credential], ParameterBindingException
+ FullyQualifiedErrorId : MissingMandatoryParameter,Microsoft.PowerShell.Commands.GetCredentialCommand
The property 'Credentials' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:39
+ ... Credential; $browser.Proxy.Credentials =[System.Net.CredentialCache]: ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (407) Proxy
Authentication Required."
At line:1 char:124
+ ... redentials; (New-Object Net.WebClient).DownloadFile('https://some.hostname ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Refer to this question Access web using Powershell and Proxy
You can try something like that in Powershell and suppose that you have already created a folder named as C:\Test:
$url = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
$file = "C:\Test\" + $url.Split("/")[-1]
$wb = New-Object System.Net.WebClient
$wb.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$wb.DownloadFile($url,$file)
EDIT : 14/08/2020 #17:08
I tried this on Windows Powershell ISE and it works 5/5 :
cls
$start_time = Get-Date
$url = "https://cdn2.unrealengine.com/Fortnite%2FBoogieDown_GIF-1f2be97208316867da7d3cf5217c2486da3c2fe6.gif"
$Folder = "$Env:Temp\DownloadFolder"
# We create a SubFolder Named "DownloadFolder" in the temporary file %Temp% if it doesn't exists yet !
If ((Test-Path -Path $Folder) -eq 0) { New-Item -Path $Folder -ItemType Directory | Out-Null }
# We can get the name of the file to be downloaded from the variable $url
# $url = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
# In our case the FileName will be = "googlelogo_color_272x92dp.png" or
# Fortnite%2FBoogieDown_GIF-1f2be97208316867da7d3cf5217c2486da3c2fe6.gif
$file = $Folder+ "\" + $url.Split("/")[-1]
Try
{
$wb = New-Object System.Net.WebClient
$wb.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$wb.DownloadFile($url,$file)
# better use Invoke-Item $Folder instead of ii
Invoke-Item $Folder
Write-Output "Running Script Time taken is : $((Get-Date).Subtract($start_time).Milliseconds) millisecond(s)"
}
Catch
{
Write-Host "Error from $url" `n"Message: [$($_.Exception.Message)"] -ForegroundColor Red -BackgroundColor DarkBlue
}
This worked for me :
powershell -Command "[System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy(); [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials; (New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt', 'file.txt')"
I just can't seem to get this to work, and I can't figure out how to google this issue. similar script is working remotly but now i need to made it work localy. But... Please check the script...
Function Local-Install {
$ComputerName = "$env:computername"
$AppName = "Deployment"
Invoke-Command -ComputerName $ComputerName ,$AppName -ScriptBlock `
{
param ($ComputerName,$AppName)
write-host "Getting Parameters for '$AppName' on $ComputerName"}
$Application = Get-WmiObject -computername $ComputerName -Namespace "root\ccm\ClientSDK" -Class CCM_Application | where {$_.Name -like "$AppName"} | Select-Object Id, Revision, IsMachineTarget
$AppID = $Application.Id
$AppRev = $Application.Revision
$AppTarget = $Application.IsMachineTarget
([wmiclass]'ROOT\ccm\ClientSdk:CCM_Application').Install($AppID, $AppRev, $AppTarget, 0, 'Normal', $False)
}
and i get an error like this:
Invoke-Command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects ins
tead of strings.
At line:5 char:1
+ Invoke-Command -ComputerName $ComputerName ,$AppName -ScriptBlock `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
+ FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand
Exception calling "Install" : ""
At line:13 char:1
+ ([wmiclass]'ROOT\ccm\ClientSdk:CCM_Application').Install($AppID, $AppRev, $AppTa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WMIMethodException
Since this is local you could just run the "env:COMPUTERNAME" call in the method. However, if you want to get this to work as is, you just need to add the -ArgumentList argument to the Invoke-Command call:
Invoke-Command -ComputerName $ComputerName, $AppName -ArgumentList $ComputerName, $AppName -ScriptBlock `
I am using the following command in PowerShell 2.0 to check if the particular folder is shared or not but I am getting an error.
[bool](Get-WmiObject -Class Win32_Share -ComputerName ravenPC -Filter "Path='D:\websites\website1'")
Also, I want to store the value of bool in a variable and check it each time if it's true or false. Can someone help me on this.
The error I get is as follows:
Get-WmiObject : Invalid query
At line:1 char:21
+ [bool](Get-WmiObject <<<< -Class Win32_Share -ComputerName ravenPC -Filter "Path='D:\websites\website1'")
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
You need to escape the backslashes in your WMI query:
[bool](Get-WmiObject -Class Win32_Share -ComputerName ravenPC -Filter "Path='D:\\websites\\website1'")
I have a simple script that assigns a drive letter to any unlettered partition, like the following:
function GetNextAvailableLetter
{
#returns an unused char for drive letter assignment, or $null if none are available
}
foreach ($disk in ( get-wmiobject -class win32_volume | where-object { $_.DriveLetter -eq $null } ) )
{
$letter = GetNextAvailableLetter
if ( $letter -ne $null )
{
$disk.DriveLetter = $letter + ":"
$disk.Put()
}
}
Oddly, sometimes it'll work, and sometimes Put() throws an exception:
Exception calling "Put" with "0" argument(s): "Not supported"
I have no idea why Put() would throw.
I made a couple of empty, driveletterless drives on my computer and was able to recreate this and one other error that I think you might have neglected to mention.
Property 'DriveLetter' cannot be found on this object; make sure it exists and is settable.
At line:2 char:1
+ $disk.DriveLetter = "Q:"
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Exception calling "Put" with "0" argument(s): "Access is denied.
"
At line:3 char:1
+ $disk.Put()
+ ~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
The way to solve this would be run the variable $disk though for each loop or something along those lines. Another approach would be to check the $disk.Count ahead of time.
$disk = get-wmiobject -class win32_volume | where-object { $_.DriveLetter -eq $null }
If (($disk) -and ($disk.Count -eq 1)){
$disk.DriveLetter = "Q:"
$disk.Put()
}
The If should in theory protect you from errors when $disk is empty or returns more that one object.
According to the Scripting Guys:
The reason for this error is that the Windows PowerShell prompt is not running with Administrator rights. Unfortunately, the error that bubbles back up from WMI does not tell us that the problem is related to rights.
Get-Service | Stop-Process -Name WSearch -WhatIf
Stop-Process : The input object cannot be bound to any parameters for
the command either because the command does not take pipeline input or
the input and its properties do not match any of the parameters that
take pipeline input. At line:1 char:15
+ Get-Service | Stop-Process -Name WSearch -WhatIf
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (fdPHost:PSObject) [Stop-Process], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.StopProcessCommand
Now from my understanding they both share the same property name "Name" so I should be able to pipe thru -Name, right?
PS C:\> Get-Service | gm
TypeName: System.ServiceProcess.ServiceController
Name MemberType Definition
---- ---------- ----------
Name AliasProperty Name = ServiceName
get-help stop-process
-Name <String[]>
Specifies the process names of the processes to be stopped. You can type multiple process names (separated by commas) or use wildcard characters.
Required? true
Position? named
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? true
So am I doing something wrong here?
Get-Service -Name wsearch | Stop-Service
will work. Filter first and pass the result through the pipeline.