Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#Requires and Advanced Function #131

Open
Schwitzd opened this issue Apr 21, 2019 · 11 comments
Open

#Requires and Advanced Function #131

Schwitzd opened this issue Apr 21, 2019 · 11 comments

Comments

@Schwitzd
Copy link

Dear all,

can you please add a chapter for #Requires in the advanced function?
I don't really understood where place it.

Regards
Schwitzd

@BaileyLawson
Copy link
Contributor

I'm not one of the "contributors" to this repo, but I might be able to help you out and I may submit a PR for this in the future...
As far as I have seen from various peoples scripts, the best practice for the #Requires statement is to put it on the first line, but it can technically go on any line (see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-6#rules-for-use). It also makes far more sense to go on the first line as the condition of #Requires must be met before the script can execute regardless of what line it is on.

@pauby
Copy link
Contributor

pauby commented Apr 24, 2019

#Requires isn't an Advanced Function directive. It's a script directive. If you've got each function in a script then the point is of course blurred but it's important to point this out.

@Schwitzd
Copy link
Author

Normally I work with functions and I would like that specific function are executed as administrator.
Best is to place the #Requires inside the function or outside?

@pauby
Copy link
Contributor

pauby commented Apr 25, 2019

@Schwitzd The #Requires directive applies to everything in the file (ie. script) that it's in.

If you want to test a function is being run from an Administrator / elevated shell then you need to write the code around that inside the function. The #Requires directive won't help you here.

Something like this might help.

@Schwitzd
Copy link
Author

@pauby thx for the link you share
If I'm not wrong if I put the #Requires inside the function and I run it as user I get an error

@pauby
Copy link
Contributor

pauby commented Apr 25, 2019

@Schwitzd If you have two functions in the same file with the #Requires you will get the error running the script, dot sourcing it or importing it (if it's a script module). The error appears for the whole file, not just your function.

script.ps1:

function function1 {
    #Requires -RunAsAdministrator
    Write-Host "function1"
}

function function2 { 
    Write-Host "function2"
}

Dot-sourcing:

> . .\script.ps1
. : The script 'script.ps1' cannot be run because it contains a "#requires" statement for running as Administrator. The current PowerShell session is not running as Administrator. Start PowerShell by using the Run as Administrator option, and then try running the script again.
At line:1 char:3
+ . .\script.ps1
+   ~~~~~~~~~~~~
+ CategoryInfo          : PermissionDenied: (script.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : ScriptRequiresElevation

Running the script:

> .\script.ps1
.\script.ps1 : The script 'script.ps1' cannot be run because it contains a "#requires" statement for running as Administrator. The current PowerShell session is not running as Administrator. Start PowerShell by using the Run as Administrator option, and then try running the script again.
At line:1 char:1
+ .\script.ps1
+ ~~~~~~~~~~~~
+ CategoryInfo          : PermissionDenied: (script.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : ScriptRequiresElevation

The #Requires affects both functions and it doesn't matter where in the file you put it.

@Schwitzd
Copy link
Author

Ok! This is really good to know. Normally I have one function for each file

What you think about to write this on the style guide?

@darkoperator
Copy link
Contributor

darkoperator commented Apr 25, 2019 via email

@pauby
Copy link
Contributor

pauby commented Apr 25, 2019

@darkoperator It's handy for putting at the top of the Module Script file. That and the Manifest are the two important module files so they will likely be opened and the #Requires statement is clearly seen.

It really depends on where your functions came from. If they are all only for a module and the module really needs them to do most of what it does, then I would suggest that a #Requires directive at the top of the module script makes sense rather than each function checking for privileges. If it's one function inside the module that does a niche task then it makes more sense to put code in the function test for the privileges.

So it's really a matter of taste I would suggest. I don't think wither way is right or wrong. Just different ways of doing the same thing.

@pauby
Copy link
Contributor

pauby commented Apr 25, 2019

What you think about to write this on the style guide?

@Schwitzd I'm not sure what you mean by that?

@Schwitzd
Copy link
Author

@pauby
I would like to summarise all this discussion in a chapter to add some where on the guideline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants