July 31, 2018 at 8:37 am
Hello everyone
how to run the get-childitem command on multiple path
In fact I have more path and I want to apply get-childiteam on these path
I have in my code $path,$path1,$path2$server="CAEHN1SQL01"
import-module sqlps
cd c:\
$path="\\Paefiler03\sql-bck$\BACKUP\PAEHN1SQL01\"
$path1="\\paefiler03\sql-bck$\BACKUP\CAEHN1SQL02\"
$path2="\\paefiler03\sql-bck$\BACKUP\EAEHN1SQL03\"
$files=Get-ChildItem -Path $path -Recurse -include *.bak | group directory | foreach {$_.group | sort LastWriteTime -Descending | select -First 1 }
foreach($file in $files)
{
$file=$file.FullName
$query="RESTORE HEADERONLY FROM DISK = '$file'"
#Invoke-Sqlcmd -ServerInstance $server -Query $query
$query_server=Invoke-Sqlcmd -ServerInstance $server -Query $query
$query_server |ForEach-Object {
$BackupName=$_.BackupName
$EncryptorType =$_.EncryptorType
$ServerName =$_.servername
}
$ServerName + " " + $BackupName +" " + $EncryptorType
}
July 31, 2018 at 7:47 pm
joujousagem2006 1602 - Tuesday, July 31, 2018 8:37 AMHello everyone
how to run the get-childitem command on multiple path
In fact I have more path and I want to apply get-childiteam on these path
I have in my code $path,$path1,$path2$server="CAEHN1SQL01"
import-module sqlps
cd c:\
$path="\\Paefiler03\sql-bck$\BACKUP\PAEHN1SQL01\"
$path1="\\paefiler03\sql-bck$\BACKUP\CAEHN1SQL02\"
$path2="\\paefiler03\sql-bck$\BACKUP\EAEHN1SQL03\"
$files=Get-ChildItem -Path $path -Recurse -include *.bak | group directory | foreach {$_.group | sort LastWriteTime -Descending | select -First 1 }foreach($file in $files)
{
$file=$file.FullName
$query="RESTORE HEADERONLY FROM DISK = '$file'"
#Invoke-Sqlcmd -ServerInstance $server -Query $query
$query_server=Invoke-Sqlcmd -ServerInstance $server -Query $query
$query_server |ForEach-Object {
$BackupName=$_.BackupName
$EncryptorType =$_.EncryptorType
$ServerName =$_.servername
}
$ServerName + " " + $BackupName +" " + $EncryptorType
}
You would want to loop through the paths which would be easier if you put those into an array rather than having a variable for each path. If you really wanted to type the paths and maintain the script for any changes, it would just be something like: $Paths = @("C:\SomePath\One","D:\SomePath\Two","\\Server\another","\\Server\Share\another")
foreach ($Path in $Paths) {
## put your code for each path in here. This will list the paths you typed in
$Path
}
It might be better to have a list of paths in a csv file. It gives you more flexibility, can be extended for other scripts, etc. If you had a basic csv file with the list of the paths, you would just do something like: $Paths = Import-CSV C:\Temp\Paths.csv
foreach ($Path in $Paths) {
## put the code to run for each path here. This will list the paths in the csv fuile
$Path
}
Sue
July 31, 2018 at 9:47 pm
You can just pipe multiple variables into a cmdlet, e.g.$path,$path1,$path2|Get-ChildItem
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy