I have for now a fixed list of items that need to be "backuped" and afterwards played back to their origin path.
It has to be done "complex" so
copy from a to b
do some stuff
copy from b to a
isn't allowed
I was thinking about to achive this with a "list of items" that is either aquired through a csv-import or a pre determind arraylist.
But i'm stuck with the part to get stuff back to their origin place while achiving this.
code actually looks like this atm.
function Backup-2Temp ($items, $Temp){
foreach ($item in $items){
Copy-Item -Path $item -Destination "$Temp"
<#
Create Array with the filename and originpath
$pathremind = @(
+= "$itemname" "$originpath"
)
#>
}
return $pathremind
}
function Restore-Temps($premind,$Temp){
$TempItems = Get-ChildItem -Path $Temp
foreach($item in $premind){
|where $item.name -EQ $TempItems.Name
copy-item -Path "itemTemppath" -Destination $item.originpath
}
}
$items2copy = @(
"$testpath\DeviceSettings.ini",
"$testpath\otc.ini"
)
$user = Read-Host "which User will be reseted"
$Temp = "C:\Users\$user\AppData\Roaming\testTemp"
$testpath = "C:\Users\$user\AppData\Roaming\test"
create-folder -path $Temp
$premind = Backup-2Temp $items2copy $Temp
Remove-Item -Path $testpath -Recurse
Restore-Temps
Remove-Folder $Temp
This explains how I was thinking about it. But I made it to complex for my Brain to get it done properly atm. And I can't figure out how to solve this mess now.
I don't need you to do my work tho. I'm just searching for any kind of help to get my mess readable again or better said correct and understandable.
Sry for the big mess here.
Edit 1:
Playing around with the "Get-Something" as mentiond in the answers. But either way I'm turning around the puzzle pieces. I won't get any data out of Get-Something my return is null everytime.
Edit 2:
Found out was missing the '-passthru', still searching for the "originpath" how to put it all together.
Edit 3:
As there was mentiond the Custom Object I had a new clue and come to this
function Backup-2Temps ($myitems){
foreach($item in $myitems){
Copy-Item -Path "$item.originpath\$item.name" -Destination $item.Temppath -PassThru
}
}
function Restore-Temps($myitems){
foreach($item in $myitems){
Copy-Item -Path "$item.Temppath\$item.name" -Destination $item.originpath -PassThru
}
}
$user = Read-Host "which User will be reseted"
$testpath = "C:\Users\$user\AppData\Roaming\test"
$otherpath = "C:\Users\$user\AppData\Roaming\testo"
$p2Temp = "C:\Users\$user\AppData\Roaming"
$Tempn = "testT"
New-Item -Path $p2Temp -Name $Tempn -ItemType "directory"
$Temp = "$p2Temp\$Tempn"
$myitems = @(
[pscustomobject]@{name="Device.txt"; originpath = $testpath; Temppath = $Temp},
[pscustomobject]@{name="otc.txt"; originpath = $testpath; Temppath = $Temp},
[pscustomobject]@{name="test.txt"; originpath = $otherpath; Temppath = $Temp}
)
Backup-2Temps $myitems
Remove-Item -Path $testpath -Force -Recurse
Remove-Item -Path $otherpath -Force -Recurse
New-Item -Path $p2Temp -Name "test" -ItemType "directory"
New-Item -Path $p2Temp -Name "testo" -ItemType "directory"
Restore-Temps $myitems
and as for now where I'm testing this I have errors comming up like this one.
Copy-Item : "Drive not found. A Drive with the name "@{name=Device.txt; originpath=C" is not found.
In T:\00Scripts\OTC-Copy\OTC-BCK-Complex(Theorie).ps1:8 character:13
+ Copy-Item -Path "$item.Temppath\$item.name" -Destination ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (@{name=Device.txt; originpath=C:String) [Copy-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
Edit 4:
For any reason, I don't know why, it gets fixed if I do it like this.
foreach($item in $myitems){
$x = $item.originpath
$y = $item.name
Copy-Item -Path "$x\$y" -Destination $item.Temppath -PassThru
}
foreachis just assign a variable to the foreach loop. You can do so like$var = foreach ($something in $somethingelse) { $obj }.