I'm running the following command:
get-childitem C:\temp\ -exclude *.svn-base,".svn" -recurse | foreach ($_) {remove-item $_.fullname}
Which prompts me very frequently like this:
Confirm
The item at C:\temp\f\a\d has children and the Recurse parameter was not specified. If you continue,
all children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
How can I have it automatically set to "A"?
The default is: no prompt.
You can enable it with -Confirm or disable it with -Confirm:$false
However, it will still prompt, when the target:
is a directory
and it is not empty
and the -Recurse parameter is not specified.
-Force is required to also remove hidden and read-only items etc.
To sum it up:
Remove-Item -Recurse -Force -Confirm:$false
...should cover all scenarios.
Add -confirm:$false to suppress confirmation.
Try using the -Force parameter on Remove-Item.
Add -recurse after the remove-item, also the -force parameter helps remove hidden files
e.g.:
gci C:\temp\ -exclude *.svn-base,".svn" -recurse | %{ri $_ -force -recurse}
Remove-Item .\foldertodelete -Force -Recurse
Just an additional tip:
Let's say the PS1 (helloworld.ps1) has a code like below:
Set-ExecutionPolicy Unrestricted -Confirm:$false -Force
Write-Host "Hello, World!"
And if we expect that each time the code runs, it would automatically run the Set-ExecutionPolicy without prompting the user & run the code silently...
It won't work that way!! I'm still figuring out how to run a PS code without prompting the user & will post the solution if I find out
You just need to add a /A behind the line.
Example:
get-childitem C:\temp\ -exclude *.svn-base,".svn" -recurse | foreach ($_) {remove-item $_.fullname} /a
Related
I'm using powershell to
Close Access Database,
Download updated Access front end to local machines,
Update (overwrite) local documents, and
Launch the Access database with parameters using cmd.exe.
Everything works fine, but the Exit command doesn't work after using cmd.exe command to launch the database.
If I comment out the cmd.exe command, then the Exit command works just fine. But if I use the Exit command, the script stops there and the Exit command does not work. Below is the entire code that I'm talking about.
## Close Microsoft Access
Stop-process -name MSACCESS -Force
## Download updated Access database to local machine
Copy-Item "F:\New_DB\Win7DBDocs\WC_Sys.mdb" -Destination "C:\DB_Docs" -Recurse -Force
## Copy Documents and Spreadsheets to local machine
echo "Overwriting files C:\DB_WPDocs"
Copy-Item -Path "F:\New_DB\DB_WPDocs\*" -Destination "C:\DB_WPDocs" -Recurse -Force
## Launch Microsoft Access with Parameters
cmd.exe /c "C:\Program Files (x86)\Microsoft Office\root\Office16\MSACCESS.EXE" "C:\DB_Docs\WC_Sys.mdb" /WRKGRP "F:\DB_Docs\Secured.mdw"
Exit
cmd.exe will block until MSACCESS.EXE exits.
To make cmd.exe launch the program and return immediately, use the start command:
cmd.exe /c start "C:\Program Files (x86)\Microsoft Office\root\Office16\MSACCESS.EXE" "C:\DB_Docs\WC_Sys.mdb" /WRKGRP "F:\DB_Docs\Secured.mdw"
... or drop cmd.exe completely and use the Start-Process cmdlet instead:
Start-Process "C:\Program Files (x86)\Microsoft Office\root\Office16\MSACCESS.EXE" -ArgumentList "C:\DB_Docs\WC_Sys.mdb", /WRKGRP, "F:\DB_Docs\Secured.mdw"
I analyse data from being sent multiple ZIP files.
They are always in this format:
service_SC30COM_####_20191130_1834.zip
#### is a random number generated by the computer.
Password is SC30COM_####, which is always part of the file name.
Any suggestions on an automation to unzip in bulk?
There is no way to do it on the command prompt without any application.
You can check this
If your remove the password, the code you need, as explain in that microsoft article should be:
$shell=new-object -com shell.application
$CurrentLocation=get-location
$CurrentPath=$CurrentLocation.path
$Location=$shell.namespace($CurrentPath)
$ZipFiles = get-childitem *.zip
$ZipFiles.count | out-default
foreach ($ZipFile in $ZipFiles)
{
$ZipFile.fullname | out-default
$ZipFolder = $shell.namespace($ZipFile.fullname)
$Location.Copyhere($ZipFolder.items())
}
If you install any application that can run on command prompt, you can extract with password. As example, for a individual file, the maximum you will get on Windows 10 is:
PowerShell Expand-Archive -Path "C:\Users\Tuffy\Desktop\PowerShell
Expand-Archive -Path "C:\Users\Whatever\Desktop\service_SC30COM_####_20191130_1834.zip"
-DestinationPath "C:\Users\Whatever\Desktop"p" -DestinationPath "C:\Whatever\Tuffy\Desktop"
Hope it helps!
You can run the following code as a bash script:
#!/bin/bash
for FILE in *.zip
do
echo "Unzipping $FILE ..."
PASSWORD=$(echo $FILE | grep -o -P '(?<=service_)[A-Za-z0-9]*_[0-9]*(?=_)')
unzip -P $PASSWORD $FILE
done
Copy the code
Paste it into FILENAME.sh
Make it executable (chmod +x FILENAME.sh)
Put it besides the zip files
Run it (./FILENAME.sh)
In az Get-AzScheduledQueryRule but in azurerm there is no query rule. I f any alternative script means pls share.
Well, the Get-AzScheduledQueryRule command gets the alert rule whose SIGNAL TYPE is Log Search, its resource type is microsoft.insights/scheduledqueryrules.
For the AzureRM module, your option is to use the Get-AzureRmResource command, specify the -ResourceType with "microsoft.insights/scheduledqueryrules".
Sample:
Get-AzureRmResource -ResourceGroupName "<ResourceGroupName>" -ResourceType "microsoft.insights/scheduledqueryrules" -Name "<alert rule name>" | ConvertTo-Json
I have a PowerShell script that would recursively loop a dir and subdir and run all SQL files inside of it and log the execution in .log files 1 for success and 1 for exceptions. The PowerShell script does what its supposed to do but in the cmd window, I see this error:
Get-Content : A parameter cannot be found that matches parameter name 'Raw'
from this line
$query = Get-Content -Path $_.FullName -Raw
This statement runs within a loop, so FullName changes per iteration. This is the version I use.
Name : Windows PowerShell ISE Host
Version : 5.0.10586.117
Sample script goes below:
Get-ChildItem $ScriptFolder -Recurse -Exclude "*Archive*" -Filter *.sql |
sort Directory |
ForEach-Object {
$query = Get-Content -Path $_.FullName -Raw
$result = SQLCMD -S $FullDBServer -E -I -Q $query -d $Database
Any thoughts?
The -Raw parameter of Get-Content was introduced in PS3.
To get file contents in one string there are several methods.
The fastest method that works in any PS version:
$text = [IO.File]::ReadAllText('c:\path\file.ext')
The 2 times slower method for PS3+:
$text = Get-Content 'c:\path\file.ext' -Raw
The 100 times slower PS2-compatible alternative:
$text = Get-Content 'c:\path\file.ext' | Out-String
The 30 times slower PS2-compatible alternative:
$text = Get-Content 'c:\path\file.ext' -ReadCount 1000 | Out-String
I've experienced this error in ps v 5 when the -path parameter wasn't actually a path.
Try adding Write-Host $_.fullname above the line throwing the error to make sure $_.fullname is actually what you think it is.
You're using PowerShell v2 or earlier. The parameter -Raw was introduced with PowerShell v3. Either upgrade PowerShell or pipe the output of Get-Content through Out-String:
$query = Get-Content -Path $_.FullName | Out-String
You should also be able to run the files directly with sqlcmd (i.e. without reading their content and passing that to the command):
$result = SQLCMD -S $FullDBServer -E -I -i $_.FullName -d $Database
I tried the following three ways to clean-up the old backup files but none of them is worked. We need your help on this to complete.
Way 1: Created Maintenance Cleanup Job, the job successful executed but backup was not deleted the files.
Way 2: Tried using xp_delete_file command.
Script Used:
EXECUTE master.sys.xp_delete_file 0,N'\\XXX\YYY',N'bak',N'2014-05-12T07:34:14'
Error Message:
Executed as user: DOMAIN\user. The process could not be created for
step 3 of job 0x307C7663CBCA6D4187B1953745E1E02F (reason: The system
cannot find the file specified). The step failed.
Way 3: Tried using the power shell script
Script Used:
$Path = "\\xxx\yyy"
$Daysback = "-30"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse -include *.bak| Where-Object { $_.LastWriteTime -lt $DatetoDelete } | remove-item -force
Error Message:
Executed as user: DOMAIN\user.. The job script encountered the
following errors.
These errors did not stop the script: A job step received an error at
line 9 in a PowerShell script.
The corresponding line is 'Get-ChildItem $Path -Recurse -include
*.txt, *.bak| Where-Object { $_.LastWriteTime -lt $DatetoDelete } | remove-item -force'. Correct the script and reschedule the job. The
error information returned by PowerShell is: 'Cannot find path
'\xxx\yyy' because it does not exist. '. Process Exit Code 0. The
step succeeded.
The Powershell script was executed successfully in powershell window but not in the powershall task. also i have created proxy account which user has permission over the shared path and used the poroxy account to run the tasks.
Please help me to solve the issue.
There needs to be a back slash at the end of the path name, so '\\XXX\YYY' needs to be '\\XXX\YYY\' (note the extra backslash) - so the command becomes:
EXECUTE master.sys.xp_delete_file 0,N'\\XXX\YYY\',N'bak',N'2014-05-12T07:34:14'