none
File Manipulation on Remote Machines RRS feed

  • Question

  • Hello Everyone,

    I'm trying to create a folder on an external list of remote machines; however, for some reason it is only completing the task on my local machine (which is in the hostlist flat file). I'm sure it is something simple that I'm missing but I'm having trouble figuring out what it is. I'm confirmed that WinRM service is running on machines. Script appears to run fine but when I go to the other remote computer to see if it created the file I ask for, it doesn't exist. It only exist on my local machine.

    SCRIPT BELOW:

    $ChangeControlNum = Read-Host 'Input Change Control Number'

    $Date = Get-Date

    $DateStr =$Date.ToString("yyyyMMdd_hhmmss")

    $FileName=$ChangeControlNum+'_'+$DateStr

    $Destination = "C:\aps\BackupFiles"

    $computers=Get-Content -Path C:\Temp\hostlist.csv

    foreach ($computer in $computers) {

    If (!(Test-Path $Destination))
    {
    New-Item -ItemType Directory -Path $Destination
    }

    If (!(Test-Path "$Destination\$FileName"))
    {
    New-Item -ItemType Directory -Path "$Destination\$FileName"
    }

    }

    Let me know if you need any additional information and thanks in advance for your assistance

    Monday, December 9, 2019 8:39 PM

Answers

  • foreach ($computer in $computers) {

    If (!(Test-Path $Destination))
    {
    New-Item -ItemType Directory -Path $Destination
    }

    If (!(Test-Path "$Destination\$FileName"))
    {
    New-Item -ItemType Directory -Path "$Destination\$FileName"
    }

    }

    The code of creating destination directory does not depend on $computer variable and work only on local host for $computers.length times


    my blog: http://shserg.ru/

    • Marked as answer by asm816 Tuesday, December 10, 2019 12:02 PM
    Tuesday, December 10, 2019 7:15 AM
  • You cannot use a local path to create a remote folder. You must use a UNC or a remoting command.

    When guessing wildly you should test your code before posting an answer.


    \_(ツ)_/

    • Marked as answer by asm816 Tuesday, December 10, 2019 12:07 PM
    Tuesday, December 10, 2019 7:19 AM

All replies

  • What is the objective of the script - i.e., what problem are you solving?

    If you need a certain directory created on machines, I would recommend configuring it through Group Policy rather than trying to script it. In this way, the directory will get created automatically on all computers to which the policy applies.


    -- Bill Stewart [Bill_Stewart]

    Monday, December 9, 2019 8:59 PM
    Moderator
  • foreach ($computer in $computers) {

    If (!(Test-Path $Destination))
    {
    New-Item -ItemType Directory -Path $Destination
    }

    If (!(Test-Path "$Destination\$FileName"))
    {
    New-Item -ItemType Directory -Path "$Destination\$FileName"
    }

    }

    The code of creating destination directory does not depend on $computer variable and work only on local host for $computers.length times


    my blog: http://shserg.ru/

    • Marked as answer by asm816 Tuesday, December 10, 2019 12:02 PM
    Tuesday, December 10, 2019 7:15 AM
  • You cannot use a local path to create a remote folder. You must use a UNC or a remoting command.

    When guessing wildly you should test your code before posting an answer.


    \_(ツ)_/

    • Marked as answer by asm816 Tuesday, December 10, 2019 12:07 PM
    Tuesday, December 10, 2019 7:19 AM
  • This is the start of me creating a backup file if it doesn't exist then backing up files before we do a deployment. s.h.s below nailed my issue.

    Thanks for your time Bill!

    Tuesday, December 10, 2019 12:06 PM