none
PS1 is not run by the task scheduler RRS feed

  • Question

  • Hello,

    I use PowerShell (4.0) and Task Scheduler to backup my virtual machines to non-domain shared folder on a Linux server:

    $date = get-date -DisplayHint date -UFormat %Y-%m-%d
    #$twoweeksago = get-date (get-date).addDays(-14) -UFormat %Y-%m-%d
    $User = "BACKUP\winbackup"
    $PWord = ConvertTo-SecureString -String "****" -AsPlainText -Force
    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
    
    $ChekFolder = "\\BACKUP\winbackup\$twoweeksago"
    $FileExists = Test-Path $ChekFolder -Credential $Credentials
    If ($FileExists -eq $True) {
    rmdir -R \\BACKUP\winbackup\$twoweeksago
    }
    
    New-Item -ItemType directory \\BACKUP\winbackup\$date\DC1
    New-Item -ItemType directory \\BACKUP\winbackup\$date\VPN1
    
    WBADMIN START BACKUP -hyperv:DC1 -backupTarget:\\BACKUP\winbackup\$date\DC1 -user:BACKUP\winbackup -password:**** -quiet
    WBADMIN START BACKUP -hyperv:VPN1 -backupTarget:\\BACKUP\winbackup\$date\VPN1 -user:BACKUP\winbackup -password:**** -quiet

    Everything was working till MS roll out an update which has disabled connection to a shared folders without any authentication. We created a user on the Linux server - but the script still works only at manual start. Editing the register isn't an acceptable solution.

    Any ideas or suggestions?

    Thank you in advance!

    • Edited by Anahaym Monday, April 15, 2019 11:02 AM
    Monday, April 15, 2019 10:59 AM

Answers

  • Remove the trailing backslash and try again.

    New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\backup\winbackup" -Credential $Credential


    • Edited by MotoX80 Monday, April 15, 2019 4:37 PM
    • Marked as answer by Anahaym Tuesday, April 16, 2019 9:21 AM
    Monday, April 15, 2019 4:36 PM
  • Thank you so much MotoX80!

    The final script is:

    $date = get-date -DisplayHint date -UFormat %Y-%m-%d
    $twoweeksago = get-date (get-date).addDays(-14) -UFormat %Y-%m-%d
    $User = "BACKUP\winbackup"
    $PWord = ConvertTo-SecureString -String "******" -AsPlainText -Force
    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
    
    New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\server\winbackup\" -Credential $Credential
    New-Item -ItemType directory B:\$date\DC1
    New-Item -ItemType directory B:\$date\VPN1
    
    $ChekFolder = "B:\$twoweeksago"
    $FileExists = Test-Path $ChekFolder
    If ($FileExists -eq $True) {
    rmdir -R B:\$twoweeksago
    }
    
    WBADMIN START BACKUP -hyperv:DC1 -backupTarget:\\server\winbackup\\$date\DC1 -quiet
    WBADMIN START BACKUP -hyperv:MASLOW -backupTarget:\\server\winbackup\\$date\VPN1  -quiet
    
    Remove-PSDrive -Name "B"
    • Marked as answer by Anahaym Tuesday, April 16, 2019 9:55 AM
    Tuesday, April 16, 2019 9:55 AM

All replies

  • What error do you get? Which statement fails?

    MS roll out an update which has disabled connection to a shared folders without any authentication.

    What update was this? I would think that it would be the target system that decides if it requires authentication or not. 

    Are you running the scheduled task using the SYSTEM account? Have you tried creating a local user in the Windows server with the same name and password as the Linux account. Set the task to run as that account.  


    Or map a B:\ drive using -credentials and then use B:\ instead of \\backup\. 
    • Edited by MotoX80 Monday, April 15, 2019 2:13 PM
    Monday, April 15, 2019 2:04 PM
  • What error do you get? Which statement fails?

    I get nothing. The task status is always "running".

    Are you running the scheduled task using the SYSTEM account?

    No, using domain admin account.

    Have you tried creating a local user in the Windows server with the same name and password as the Linux account

    Not yet, I would like to avoid to have one more account.

    What update was this?

    Unfortunately, I don't remember but it is an update for all systems: Windows 10 and Servers. It came I guess several months ago, but was installed on this server on 20th March. I just found a solution - edit the register, but I don't want to change it on the each computer or server.
    Monday, April 15, 2019 2:16 PM
  • Did you configure the task to run "powershell.exe" plus arguments or just "yourscript.ps1"? Look in task manager to see if you have notepad.exe's running. 

    The first step is to configure logging so that you know what the task does. Create a bat file that contains:

    powershell.exe c:\yourfolder\yourscript.ps1 1 > c:\yourfolder\Logs\backup%date:~12,2%%date:~4,2%%date:~7,2%.log 2>&1
    Change the task to run the bat file. Run the task and look in the log to see its console output. 

    Monday, April 15, 2019 2:34 PM
  •  I just found a solution - edit the register, but I don't want to change it on the each computer or server.

    It would be nice if you would share that information with everyone who might be reading this thread. 
    Monday, April 15, 2019 2:40 PM
  • it says:

    New-Item : The user name or password is incorrect.

    I added the credentials and then got another error:

    New-Item : Cannot retrieve the dynamic parameters for the cmdlet. The FileSystem provider supports credentials only on the New-PSDrive cmdlet. 
    Perform the operation again without specifying credentials.
    It means, I can't create a folder on the remote share due to authentications problem...
    Monday, April 15, 2019 2:51 PM
  • https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive?view=powershell-6

    Use new-psdrive to create a "B:" drive to the Linux server. Change the script to use B:\ instead of \\backup\

    Monday, April 15, 2019 2:56 PM
  • New-PSDrive mounts an existing shared folder, but I need to create a new folder...

    I even can't mount the drive:

    New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\backup\winbackup\" -Credential $Credential


    New-PSDrive : The system cannot find the path specified At C:\scripts\backup.ps1:13 char:1 + New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\backup\winbackup ...

    The same error during manual start of the command.

    Monday, April 15, 2019 3:10 PM
  • Is this an SMB1 problem? Is that the registry change that you alluded to? 

    Ask your Linux admin if his server supports SMB2.  


    https://blogs.technet.microsoft.com/filecab/2016/09/16/stop-using-smb1/
    • Edited by MotoX80 Monday, April 15, 2019 3:28 PM
    Monday, April 15, 2019 3:27 PM
  • the server uses SMBv3:

    @backup:~# smbstatus
    Samba version 4.2.14-Debian
    PID     Username      Group         Machine            Protocol Version
    ------------------------------------------------------------------------------
    23369     winbackup     winbackup     10.255.9.22  (ipv4:10.255.9.22:13872) SMB3_00
    Monday, April 15, 2019 4:24 PM
  • Lets start with the basics, can you "ping backup" and does is show 10.255.9.22 as the IP?

    Using the Windows explorer, can you browse \\backup\winbackup?

    Monday, April 15, 2019 4:31 PM
  • Remove the trailing backslash and try again.

    New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\backup\winbackup" -Credential $Credential


    • Edited by MotoX80 Monday, April 15, 2019 4:37 PM
    • Marked as answer by Anahaym Tuesday, April 16, 2019 9:21 AM
    Monday, April 15, 2019 4:36 PM
  • Thank you so much MotoX80!

    The final script is:

    $date = get-date -DisplayHint date -UFormat %Y-%m-%d
    $twoweeksago = get-date (get-date).addDays(-14) -UFormat %Y-%m-%d
    $User = "BACKUP\winbackup"
    $PWord = ConvertTo-SecureString -String "******" -AsPlainText -Force
    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
    
    New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\server\winbackup\" -Credential $Credential
    New-Item -ItemType directory B:\$date\DC1
    New-Item -ItemType directory B:\$date\VPN1
    
    $ChekFolder = "B:\$twoweeksago"
    $FileExists = Test-Path $ChekFolder
    If ($FileExists -eq $True) {
    rmdir -R B:\$twoweeksago
    }
    
    WBADMIN START BACKUP -hyperv:DC1 -backupTarget:\\server\winbackup\\$date\DC1 -quiet
    WBADMIN START BACKUP -hyperv:MASLOW -backupTarget:\\server\winbackup\\$date\VPN1  -quiet
    
    Remove-PSDrive -Name "B"
    • Marked as answer by Anahaym Tuesday, April 16, 2019 9:55 AM
    Tuesday, April 16, 2019 9:55 AM