How do I make this command work??

I’m working on a script to copy all changelogs to a central location.
I’m using this command:

cls # backup all initial configs and changelogs $servers = @('accumedic') foreach ($server in $servers) { $svrname2 = $server + "_changelog.txt" # write-host $svrname2 -- this works test-path \\$server\C`$\$svrname2 -- this returns false copy \\$server\C`$\$svrname2 \\mls-storage2\software\changelogs }

How do I make this copy command work??
copy \$server\C`$$svrname2 \mls-storage2\software\changelogs

The file and path DO exist.

I get this error:

At D:\software\Scripts\backup_changelogs.ps1:11 char:5

  • copy <<<< \$server\C`$$svrname2 \mls-storage2\software\changelogs
    • CategoryInfo : ObjectNotFound: (\accumedic\C$\accumedic_changelog.txt:String) [Copy-Item], ItemNotFoundExcepti
      on
    • FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

Thank you, Tom

So you’ve verified that \accumedic\C$\accumedic_changelog.txt exists? Because the shell appears to feel pretty strongly that it doesn’t.

I know that file exists because I created it myself and I can see it in the C:\ of accumedic.
Why does Powershell think it does not exist??
Is it because I’m running the script on my local domain computer??
How do I make Powershell behave?? :slight_smile: :slight_smile:
Thank you, Tom

Well, I guess I’m asking, can you hit Windows+R, type in \accumedic\C$\accumedic_changelog.txt, and have the file open? From the same computer that you’re using to run PowerShell?

And if that works, are you running PowerShell on the same machine, using the same user account?

PowerShell just asks the OS to open the file; it’s the OS that’s saying it can’t find the file. PowerShell is just passing along the bad news.

You need to concantenate a string. Personally, I like using string format:

$servers = @("accumedic")
foreach ($server in $servers) {
    $path = "\\{0}\C$\{0}_changelog.txt" -f $server
    if (test-path $path) {
        Copy-Item $path \\mls-storage2\software\changelogs
    }
} 

There are many ways to do, but you just need to build a string that contains the variable:

$servers = @("accumedic")
foreach ($server in $servers) {
    #String Format
    "\\{0}\C$\{0}_changelog.txt" -f $server
    #Standard concantenation
    "\\" + $server + "\C$\" + $server + "_changelog.txt"
}

Output:

\\accumedic\C$\accumedic_changelog.txt
\\accumedic\C$\accumedic_changelog.txt

@Don – OIC – Now I’m getting closer.
I have realized that running this script from another server where I’m logged in as administrator will make it work, now to get the other servers worked into the script and so forth.
It won’t work from my local computer.
Thank you, Tom

Well, most of the time it works from one of the servers where I’m logged in as administrator, but it does not work for SOME servers???
Puzzling!!
Thank you, Tom

Well… look, I think this might be a misunderstanding about how file shares work.

C$ is an Admin share. It won’t usually show up in Explorer - it’s hidden. And it’s usually permission only for Administrators. So \server won’t usually show it; \server\c$ should work. And if that doesn’t work in Explorer, it isn’t going to work in PowerShell, but it’s a little tough for me to help you fix that from here.

I know C$ is an Admin share, the puzzling thing is the Administrator account can get at it on some of the servers but not on other servers.
I must compare C: between the ones that work and the ones that don’t work, which may point me toward getting things sussed.
Thank you, Tom

It’s likely either a permission problem, or your credential isn’t being recognized by the server. And yeah - comparing working vs not working is definitely the right step. Good luck!

File and Printer Sharing had to be enabled on the network adapters of the servers that were initially not accessible, then the script worked correctly as expected.
Thank you, Tom