0

Trying to push DSC I'm hitting the following error:

Failure to get a valid result from the execution of TestScript. The Test script should return True or False.

Here's the TestScript:

return (Test-Path -Path "FullPath:\To\File")

A couple things I've tried:

  1. The Script resource has a (unmanaged) service account's credentials specified in the Credential parameter. Thinking it might not have permissions to the directory, causing Test-Path to error, I launched powershell as the user on the target machine and ran the cmdlet. It returned False (as expected). I've since made sure that the configuration gives the account permissions to the folder anyways.
  2. Thinking it might be some weird idiosyncrasy with returning the cmdlet, I tried assigning the cmdlet to a variable and returning that. No dice.

Any ideas would be appreciated.

Edit: Here's the full resource, for those curious. It's basically just a couple quick lines to pull a script out of source control and place it locally so that I can create a scheduled task to run said script. Casting the result to a bool didn't work (same error). I'm wondering if it's even getting inside the TestScript at this point...checking get-executionpolicy shows it as undefined for the account but at the userpolicy, machinepolicy and localmachine level they're all bypass.

Script NameOfScript {
    DependsOn = "[cNtfsPermissionEntry]DirectoryPermissions"
    Credential = $serviceAccountPSCredentialObject
    SetScript = {
        Import-Module -Name Subversion
        New-SvnWorkingCopy -Url "https://svnrepourl/script.ps1" -Path "E:\Scripts\"
    }
    TestScript = {
        [bool]$result
        $result = Test-Path -Path "E:\Scripts\script.ps1" -ErrorAction Stop
        return $result
    }
    GetScript = { }
}
Matt
  • 1,230
  • 16
  • 19
ndarwincorn
  • 19
  • 2
  • 8
  • Do you have any PowerShell running before your `return`? If so, that PowerShell may be corrupting your output if it has output of its own. See [How to return one and only one value from a PowerShell function?](http://stackoverflow.com/q/29556437/5220391). – Matt Jan 12 '17 at 19:46
  • Your whole script resource might be useful to understand the concept. – TravisEz13 Jan 12 '17 at 19:47
  • The TestScript is just that one-liner, which is partly why it's so confusing that it's not getting a boolean result. I'll edit the question with the whole thing while I test casting the result to a boolean. – ndarwincorn Jan 12 '17 at 20:00
  • The `credential` is not used. What version of PowerShell are you running? You can find it using by running `$PSVersionTable` – TravisEz13 Jan 12 '17 at 20:58
  • 4.0. So Credential as a parameter exists but isn't implemented for the Script resource? – ndarwincorn Jan 12 '17 at 21:05
  • No, `Credential` there but you have to actually use it in your script. In 5.0, `PsDscRunAsCredential` is more appropriate. – TravisEz13 Jan 13 '17 at 21:00
  • Examining the [source](https://github.com/PowerShell/xPSDesiredStateConfiguration/blob/dev/DSCResources/MSFT_xScriptResource/MSFT_xScriptResource.psm1) for the Script resource, I'm wondering if we're even talking about the same thing? For one, I'm not seeing `PsDscRunAsCredential` anywhere. Additionally, the resource clearly runs the script in the manner detailed in my answer when the `Credential` parameter is specified. So yes, it's being used. – ndarwincorn Jan 13 '17 at 21:30

2 Answers2

0

Try this

return ([bool]$testPath = Test-Path -Path "FullPath:\To\File")
YanivK
  • 116
  • 1
  • 5
-1

Figured it out, with the help of this forum post. Initially I didn't think it'd be much help since I shouldn't be experiencing double-hop issues, but I'll explain why it's germane below. @TravisEz13 made the comment that the Credential parameter isn't used, but that is incorrect.

If you look at the Script resource, when you specify credentials this is how it runs the script blocks:

$scriptExecutionResult = Invoke-Command -ScriptBlock $ScriptBlock -ComputerName . -Credential $Credential

The service account in question doesn't have remote access to the machine. So when I launch powershell locally as that user and run the Test-Path cmdlet, it works, but when I try to run the above Invoke-Command with that account's creds, it returns an access denied error.

My solution was to write a module/resource for subversion checkout. Not just to get around this, but also because the subversion powershell module I was using above doesn't provide a means to pass credentials to the svn binary.

ndarwincorn
  • 19
  • 2
  • 8
  • My comment about the $credential parameter was in regards to his sample, not in general. You are taking my comment out of context. – TravisEz13 Jan 13 '17 at 20:49
  • Even with the little additional context you've provided, your comment still doesn't make sense. See my reply on your answer. – ndarwincorn Jan 16 '17 at 19:15