I'm trying to write a script on a Windows 2008 R2 server to change the contents of an HTM file (Statuspage.htm) from "permit_connections=yes" to "permit_connections=no". I get the following error:
Get-Process : A positional parameter cannot be found that accepts argument
'permit_connections=no '.
At line:1 char:1
+ PS C:\> ((Get-Content -path D:\inetpub\wwwroot\statuspage.htm -Raw) - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand
Get-Process : A positional parameter cannot be found that accepts argument
'Get-Content'.
At line:2 char:1
+ PS C:\> Get-Content -Path (D:\inetpub\wwwroot\statuspage.htm)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand
Following is my script:
PS C:\> ((Get-Content -Path D:\inetpub\wwwroot\statuspage.htm -Raw) -replace 'permit_connections=yes','permit_connections=no') | Set-Content -Path D:\inetpub\wwwroot\statuspage.htm
PS C:\> Get-Content -path D:\inetpub\wwwroot\statuspage.htm
Related
I have a script thats been running fine for months but over the last few days when I run it I'm getting this error - any help in debugging it?
here's line 66 in the code:
$remotefilehash = ($remoteFiles | Where-Object { -not ($_ | Select-String -Quiet -NotMatch -Pattern '^[a-f0-9]{32}( )') } -replace '^[a-f0-9]{32}( )', '$0= ' -join "`n") | ConvertFrom-StringData
I wonder if for some reason $remoteFiles is zero ? ie null and hence throws this error?
Where-Object : A parameter cannot be found that matches parameter name 'replace'.
At P:\scripts\code\pcloud_sync.ps1:66 char:125
+ ... ing -Quiet -NotMatch -Pattern '^[a-f0-9]{32}( )') } -replace '^[a-f0 ...
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Where-Object], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.WhereObjectCommand
You cannot call a method on a null-valued expression.
At P:\scripts\code\pcloud_sync.ps1:74 char:1
+ $diffmd5 = $remotefilehash.GetEnumerator().Where({ -not $localHashSet ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
UPDATE2
after adding the parthensis now getting another new error on the same line:
ConvertFrom-StringData : Data item 'a3512c98c9e159c021ebbb76b238707e' in line 'a3512c98c9e159c021ebbb76b238707e = My
Pictures/Tony/Automatic Upload/Tony’s iPhone/2022-10-08 21-46-21 (2).mov' is already defined.
At P:\scripts\code\pcloud_sync.ps1:66 char:179
+ ... ace '^[a-f0-9]{32}( )', '$0= ' -join "`n") | ConvertFrom-StringData
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [ConvertFrom-StringData], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.ConvertFromStringDataCommand
You cannot call a method on a null-valued expression.
At P:\scripts\code\pcloud_sync.ps1:74 char:1
+ $diffmd5 = $remotefilehash.GetEnumerator().Where({ -not $localHashSet ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
I am trying to move 34k images from a large dir that has 500k images. I only need the ones on the csv I created. the code I wrote call the item but doesn't move them. I wrote the code below to test on my desktop before touching production images (10 images but only want 5 to move). I am new to scripting, appologies for any mistakes I made. Thank you for any help that can be provided. I have learned a lot on this site.
Get-Content -path "C:\Users\waemisegger\Desktop\test1.csv" | ForEach-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports - Copy\images" -Recurse $_ -Destination "C:\Users\waemisegger\Desktop\test" }
this is the error I get:
PS C:\Windows\system32> Get-Content -path "C:\Users\waemisegger\Desktop\test1.csv" | ForEach-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports - Copy\images" -Recurse $_ -Destination "C:\Users\waemisegger\Desktop\test" }
Copy-Item : A positional parameter cannot be found that accepts argument 'FullName '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument '-------- '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-087.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-097.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-106lf.png'.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-118.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : A positional parameter cannot be found that accepts argument 'C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-159.png '.
At line:1 char:80
... ch-Object {Copy-Item -Path "C:\Users\waemisegger\Desktop\SSL reports ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
here is what the csv looks like:
FullName
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-087.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-097.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-106lf.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-118.png
C:\Users\waemisegger\Desktop\SSL reports - Copy\images\10-159.png
Edited to show before & after folder contents.
Assuming your CSV file looks like this:
Source
------
apple-ABC.doc
apple-ABC.pdf
Then this short script will do the work.
$MoveList = Import-Csv -path "G:\Test\FilesToMove.csv"
For ($Cntr = 0; $Cntr -lt $MoveList.Count; $Cntr++) {
$MIArgs =
#{Path = "G:\Test\$($MoveList[$($Cntr)].Source)"
Destination = "G:\Test\Music"}
Move-Item #MIArgs
}
Note if your CSV has the paths you just need to delete the test path [G:\Test] from the Path argument. Of course you'll provide appropriate paths for all items in my example.
Folders before running code:
Folders after running code:
HTH
I'm trying to export my entire Cert:\CurrentUser\Root\ certificate store to an .SST file. What am I doing incorrectly?
In an administrator's powershell - I'm using:
Get-ChildItem -Path Cert:\CurrentUser\Root\ | Export-Certificate -FilePath C:\certs\test.sst -Type SST.
It returns:
Export-Certificate : The object or property already exists. (Exception
from HRESULT: 0x80092005) At line:1 char:46
+ ... User\root | Export-Certificate -FilePath c:\certs\allcertasdfs.sst -T ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Export-Certificate], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.CertificateServices.Commands.ExportCertificateCommand
I am attempting to run a powershell script from my PC against a txt file that contains numerous remote PC's. I want to run a script against that list of PC's that will search a certain file path for specific file name and/or file extension.
I have been attempting to do a for each loop with my txt file inserted as a variable. I am also attempting to get the file names returned to me via the cmdlet "Get-ChildItem."
I have not been able to get anything to work correctly so far. Can anyone point me in the right direction with this? Below are a couple of things I have tried so far...
**
PS C:\Windows\system32> $name= gc env:computername
$computers= get-content -path c:\users\person\Desktop\book2.txt
$csvfile = "c:\temp\$name.csv"
foreach ($computer in $computers) {Get-ChildItem -recurse -filter "C:\Users\*.locky"}
export-csv -filepath $csvfile
**
*
Get-ChildItem : Second path fragment must not be a drive or UNC name.
Parameter name: path2
At line:4 char:36
+ foreach ($computer in $computers) {Get-ChildItem -recurse -filter "C:\Users\*.lo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (C:\Windows\system32:String) [Get-ChildItem], Argume
ntException
+ FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand
Export-Csv : A parameter cannot be found that matches parameter name 'filepath'.
At line:5 char:12
+ export-csv -filepath $csvfile
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand
*
AND
C:\Windows\system32> ForEach ($system in (Get-Content C:\temp\Book2.txt))
if ($exists in (Test-Path \\$system\c$\Users\*.locky))
{
Get-Command $exists | fl Path,FileVersion | Out-File c:\temp\results.csv -Append
}
At line:1 char:53
+ ForEach ($system in (Get-Content C:\temp\Book2.txt))
+ ~
Missing statement body in foreach loop.
At line:3 char:14
+ if ($exists in (Test-Path \\$system\c$\Users\*.locky))
+ ~~
Unexpected token 'in' in expression or statement.
At line:3 char:14
+ if ($exists in (Test-Path \\$system\c$\Users\*.locky))
+ ~~
Missing closing ')' after expression in 'if' statement.
At line:3 char:55
+ if ($exists in (Test-Path \\$system\c$\Users\*.locky))
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingForeachStatement
Wrong -filter parameter in Get-ChildItem cmdlet. Next code snippet should work:
foreach ($computer in $computers) {"\\$computer\C$\Users"|`
Get-ChildItem -recurse -filter "*.locky"}
For instance, consider the following console transcript:
PS C:\dev\windows> rmdir -Recurse .\bin
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Implementation\Common.DTO.XML: Access to the path 'Common.DTO.XML' is denied.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : PermissionDenied: (Common.DTO.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug\Implementation cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (Implementation:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.Contract.XML: Access to the path 'Shunra.Common.Contract.XML' is denied.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : PermissionDenied: (Shunra.Common.Contract.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.XML: Access to the path 'Shunra.Common.XML' is denied.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : PermissionDenied: (Shunra.Common.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (Debug:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (DotNet:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<< -Recurse .\bin
+ CategoryInfo : WriteError: (C:\dev\windows\bin:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
PS C:\dev\windows>
Now compare it with the ordinary shell (cmd.exe):
C:\dev\windows>rmdir /s/q bin
bin\DotNet\Debug\IMPLEM~1\Common.DTO.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.Contract.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.XML - Access is denied.
C:\dev\windows>
The difference is obvious and I like the laconicity of cmd.exe much much more than the verbosity of the powershell.
Can I have the same laconicity in powershell? If not for all the commands then maybe just for the Remove-Item, which I use often?
The closest result you can get is by changing the global $ErrorView value to "CategoryView". Another way is to create your own view.