-1

I'm a beginner and i tried to make a simple script to save our current scripts on other admin computer.

So:

$SAVE = Get-ADComputer -filter " name -notlike '* *mycomputer*' " -SearchBase 'OU=Supervision,OU=...'

Foreach ($S in $SAVE)

{

Copy-Item 'd:\doc\*' -destination '\\$S\d$\test'

}

I tried selecting the Name (adding a pipe after the get-adcomputer) but it fail and trying the line copy item without the for each work so i don't know where i went wrong. If someone could point me the problem?

I tried Test-Path \\$A\D$ and it returned a true

Thank

  • your `$S` variable should have an ADObject in it ... and that is not likely to work in your destination path. [*grin*] have you looked at the value of your destination in each iteration of the foreach? – Lee_Dailey Dec 03 '19 at 12:54
  • My `$S` variable contain the name of my computer. When used separatly form the `Foreach` it work but when used in the loop, it doesn't. – Maniacdemanga Hitomi Dec 03 '19 at 13:03
  • Try `-destination "\\$S\d$\test"` instead of `-destination '\\$S\d$\test'`. When you enclose a string in double quotation marks (a double-quoted string), variable names that are preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing. _When you enclose a string in single-quotation marks (a single-quoted string), the string is passed to the command exactly as you type it. No substitution is performed!_ – JosefZ Dec 03 '19 at 13:19
  • @JosefZ I already tried it. I also added a `echo $A` To make sure the loop was working. – Maniacdemanga Hitomi Dec 03 '19 at 13:28
  • I can't see `$A` inside the loop. Try `Test-Path "\\$S\d$\test"` there. – JosefZ Dec 03 '19 at 13:34
  • @JosefZ i found a way to make it work: I deleted the `"` . I did not replace them and to make sure it would work created a new variable in the loop `$tst = $S.IPv4Address` and replaced the `$S` in the path name by `$tst` so now it's like that : `Copy-Item 'd:\doc\*' -destination \\$tst\d$\test` it seems to work. – Maniacdemanga Hitomi Dec 03 '19 at 13:41
  • @ManiacdemangaHitomi - your code as posted will not put the computer NAME in the `$S` variable. it will put the entire ADObject ... and that likely will mean that your path will not be valid since it _expects_ the `$S` to be just a computer name, not an ADObject. – Lee_Dailey Dec 03 '19 at 16:20

1 Answers1

0

i found a way to make it work: I deleted the " . I did not replace them and to make sure it would work created a new variable in the loop $tst = $S.IPv4Address and replaced the $S in the path name by $tst so now it's like that : Copy-Item 'd:\doc\*' -destination \\$tst\d$\test it seems to work.