{"action":"opened","issue":{"url":"https://api.github.com/repos/PowerShell/PowerShell/issues/2516","repository_url":"https://api.github.com/repos/PowerShell/PowerShell","labels_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/2516/labels{/name}","comments_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/2516/comments","events_url":"https://api.github.com/repos/PowerShell/PowerShell/issues/2516/events","html_url":"https://github.com/PowerShell/PowerShell/issues/2516","id":184414256,"number":2516,"title":"Request Make #Requires script metalanguage conditional","user":{"login":"Sarafian","id":743467,"avatar_url":"https://avatars.githubusercontent.com/u/743467?v=3","gravatar_id":"","url":"https://api.github.com/users/Sarafian","html_url":"https://github.com/Sarafian","followers_url":"https://api.github.com/users/Sarafian/followers","following_url":"https://api.github.com/users/Sarafian/following{/other_user}","gists_url":"https://api.github.com/users/Sarafian/gists{/gist_id}","starred_url":"https://api.github.com/users/Sarafian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Sarafian/subscriptions","organizations_url":"https://api.github.com/users/Sarafian/orgs","repos_url":"https://api.github.com/users/Sarafian/repos","events_url":"https://api.github.com/users/Sarafian/events{/privacy}","received_events_url":"https://api.github.com/users/Sarafian/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-10-21T07:09:13Z","updated_at":"2016-10-21T07:09:13Z","closed_at":null,"body":"This is regarding the [about_Requires](https://technet.microsoft.com/en-us/library/hh847765.aspx).\r\nCurrently if any of them appear anywhere in the script, then the condition is applied. \r\n\r\nFor example both following scripts will raised the error \r\n\r\n```\r\n#Requires -RunAsAdministrator\r\nif($false)\r\n{\r\n   Write-Host \"Only this would require admin rights\"\r\n}\r\n```\r\nand\r\n```\r\nif($false)\r\n{\r\n   #Requires -RunAsAdministrator\r\n   Write-Host \"Only this would require admin rights\"\r\n}\r\n```\r\n\r\nI understand that the above example is a bit dummy in terms of usefulness but this is what I really have in mind.\r\nI'm building scripts that have the potential to execute a certain logic locally or remotely. When the execution is local I want to block non-administrators like the `#Requires -RunAsAdministrator` does. But when its a remote execution, I would like my script to execute and the remote execution to raise an error same as with the `#Requires -RunAsAdministrator`. And I would like the same reasoning to be applied for the rest of the `#Requires` statements in [about_Requires](https://technet.microsoft.com/en-us/library/hh847765.aspx).\r\n\r\nHere is a small example\r\n\r\n```\r\n$block={\r\n    #Requires -RunAsAdministrator\r\n   Write-Host \"You are an administrator\"\r\n}\r\n\r\n# Prevent local execution\r\nInvoke-Command -ScriptBlock $block\r\n\r\n# Prevent remote execution in the remote session.\r\nInvoke-Command -ComputerName MECULAB12201 -ScriptBlock $block\r\n```\r\n\r\nBecause of the issue described above this is not possible and the entire script's execution is rejected.\r\n\r\nI know about validating with .net language but it would be nice if PowerShell offered native support for this. Example code\r\n\r\n```powershell \r\n([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] \"Administrator\")\r\n```\r\n\r\nMy current alternative for local execution is to isolate the `#Requires -RunAsAdministrator` in a script e.g. `Test-Administrator.ps1` and conditionally execute it from the main script. It's a bit like using a cmdlet with C# style implementation but I prefer to use the native concepts when possible.\r\n\r\n"}}
{"actor":{"display_login":"Sarafian"}}