Copy Folder and add Registry Key/Value

This topic contains 8 replies, has 3 voices, and was last updated by Profile photo of fair64 fair64 2 years, 4 months ago.

  • Author
    Posts
  • #17869
    Profile photo of fair64
    fair64
    Participant

    I am Trying to copy a folder from a UNC path to a remote PC and add registry Key and value on that remote PC also. The Registry piece seems to work but the file copy dose not. Any ideas?
    Thanks

    $servername = "serv1"

    copy-item -Path "\\shares\home\user\AdminTools\bginfo\" -Recurse -Destination "\\$servername\c$\BGInfo"

    Invoke-Command -ComputerName $servername -ScriptBlock {

    $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $servername)
    $key = $baseKey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", $true)
    $key.SetValue('bginfo', 'c:\\bginfo\\bginfo.bat')
    }

  • #17871
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    I just tried it and it did seem to work for me so might not necessarily be the code.

    Do you get any error messages?

  • #17885
    Profile photo of fair64
    fair64
    Participant

    Looks like it might be a problem with Windows 2012 , it does seem to work with 2008. On 2012 it creates an object on the root of C with the folder name but is not a folder. No errors...

  • #17886
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    hmm that's strange as i tested on server 2012 too

  • #17888
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    On windows 2012 File and Print sharing is not enabled by default. that means your administrative shares like \\server\c$ are not available. That's why your copy fails.
    To enable file and print sharing on Server 2012 you can try:

        Invoke-Command -ComputerName MyServer { netsh advfirewall firewall set rule group=”File and Printer Sharing” new enable=Yes } 
    
  • #17892
    Profile photo of fair64
    fair64
    Participant

    Thanks Sam tried that but didn't change its behavior..
    just creates a 7KB inaccessible file called BGInfo. In addition noticed that the registry key creation and value work on 2012 but not 2008? Ugh
    In the 2008 Registry issue I do reciev an error, but WinRM service is running...

    ERROR: the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.
    ERROR: + CategoryInfo : OpenError: (tools01:String) [], PSRemotingTransportException
    ERROR: + FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken
    ERROR: [tools01] Connecting to remote server tools01 failed with the following error message : The client cannot connect to the destination specified in the request.
    ERROR: Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on
    ERROR: the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure
    ERROR: the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.
    ERROR: + CategoryInfo : OpenError: (tools01:String) [], PSRemotingTransportException
    ERROR: + FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken

  • #17894
    Profile photo of fair64
    fair64
    Participant

    So ran winrm quickconfig on the 2008 server and modified the listener, now that works but is there a way to enable that via powershell?

  • #17896
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    the lines

    $servername = "serv1"
    copy-item -Path "\\shares\home\user\AdminTools\bginfo\" -Recurse -Destination "\\$servername\c$\BGInfo"
    

    have been tested to work as intended from server 2012 r2 to server 2012 r2

  • #17897
    Profile photo of fair64
    fair64
    Participant

    Thanks Sam must be something with our Windows set up...

You must be logged in to reply to this topic.