I am working with a script that prompts the user to select a folder or folders. The code follows.
$wrkfolder = "C:\MyPC\WorkingFolder" #workfolder starting location
$app = New-Object -ComObject shell.Application
# Display Browse for File to select a folder or ALL folders
$folder = $app.BrowseForFolder(0,"Select Folder -- Cancel to select ALL",0,$wrkfolder)
if ($folder.self.path -ne "") {
$fldr = $folder.Self.Path
$fldrName = $folder.Self.Name
}
Currently, that prompts the user with a folder browse input dialog, and if the user selects CANCEL, it will select all the folders in the $wrkfolder. Or, if they select a single folder, it will return that folder. If the user closes the dialog, it returns $null (which is the same thing as selecting Cancel).
I'd like for it the script to allow the user to have the option to select a folder, select multiple folders, select ALL folders, and for the Cancel to actually close the dialog & return a value that I could then exit the script.
Without re-inventing the wheel on this form, is there an easy way to prompt the user with a browse input folder dialog & allow them to select 1+ folders, all folders or to cancel the script? There has to be a better way to do this than what I've got thus far.
This won't help you unless you're running the PowerShell V3 beta but for future reference, you can use the Out-GridView's new PassThru parameter e.g.:
Get-ChildItem -Directory | Out-GridView -PassThru | ...
Out-GridView allows multi-selection and you can also cancel the whole pipeline operation via the cancel button.
Related
I have a working script to disable 4 usb joysticks using their respective InstanceID's
$pnpIds = 'HID\VID_0079&PID_0006\7&1699A0E&198&0000', 'HID\VID_0079&PID_0006\7&5438EB5&19D&0000', 'HID\VID_0079&PID_0006\7&390C5738&17D&0000','HID\VID_0079&PID_0006\7&2652A693&16C&0000'
foreach ($pnpId in $pnpids) {
Disable-PnpDevice -InstanceId $pnpId -Confirm:$false
}
It works fine when executing, problem is upon reboot the ID's change.. only 3 characters change at the end in between the "&" characters (HID\VID_0079&PID_0006\7&1699A0E&198&0000
The rest remains the same. Anyway to use wildcards for those 3 characters?
If not is there a way to write a script that will fetch the current InstanceID's for the USB joysticks then disable/enable them with the script I currently am using? Way out of my league here..
Here is the solution from Capt. Whale at Super User. Thankyou!
$pnpIds =
'HID\VID_0079&PID_0006\7&1699A0E&*&0000',
'HID\VID_0079&PID_0006\7&5438EB5&*&0000',
'HID\VID_0079&PID_0006\7&390C5738&*&0000',
'HID\VID_0079&PID_0006\7&2652A693&*&0000'
foreach ($pnpId in $pnpids) {
Get-PnpDevice -InstanceID $pnpId |
Where Status -Like 'OK' |
Disable-PnpDevice -Confirm:$false
}
To enable - Enable-PnPDevice
Then replace - Where Status -Like 'OK' To - Where Status -Like 'Error'
The status of the device will be 'error' since it was disconnected.
Link to original thread for the solution -
https://superuser.com/questions/1682707/script-to-disable-enable-pnp-device-using-instanceids-but-the-ids-change-upo
I am working on a GUI project over here, my issue is related with disabling a submit button. The GUI is a Wizard and in some pages, there would be more than one Submit Buttons.
Whenever the user clicks on any of the Submit Button, some verifications will be done and if everything is good the next Input box will be enabled (as it is disabled by default in the XAML). when the user changes the text of the second Input Box, its Submit Button allocated for this Input Box will be enabled.
To make sure the user will not do something wrong, I want to disable the first Submit Button when the user changes the text of the second input box.
At this point, Whenever the user changes the text of an input box, the corresponding submits button gets enabled fine with no issues but the previous submit button do not get disabled.
Below is a PIC of one of the pages and as you can see the second submit button get enabled but the first did not get disabled.
My code is as follow:
# Main Page Input Boxes.
# Enable Submit Buttons.
$Global:GuiHash.WizardMainPageInputBox1.Add_TextChanged({$Global:GuiHash.WizardMainPageSubmitButton1.IsEnabled="True"})
$Global:GuiHash.WizardMainPageInputBox2.Add_TextChanged({$Global:GuiHash.WizardMainPageSubmitButton2.IsEnabled="True"})
$Global:GuiHash.WizardMainPageInputBox3.Add_TextChanged({$Global:GuiHash.WizardMainPageSubmitButton3.IsEnabled="True"})
# Clear default text from the box and disable priviouse submit buttons.
$Global:GuiHash.WizardMainPageInputBox1.Add_GotFocus({If ($Global:GuiHash.WizardMainPageInputBox1.Text -eq "NSX Manager FQDN:") {$Global:GuiHash.WizardMainPageInputBox1.Dispatcher.Invoke([action]{$Global:GuiHash.WizardMainPageInputBox1.Text = ""})}})
$Global:GuiHash.WizardMainPageInputBox2.Add_GotFocus({
Wait-Debugger
If ($Global:GuiHash.WizardMainPageInputBox2.Text -eq "NSX Manager Admin Password:") {$Global:GuiHash.WizardMainPageInputBox2.Dispatcher.Invoke([action]{$Global:GuiHash.WizardMainPageInputBox2.Text = ""})}
$Global:GuiHash.WizardMainPageSubmitButton1.IsEnabled="False"
})
$Global:GuiHash.WizardMainPageInputBox3.Add_GotFocus({If ($Global:GuiHash.WizardMainPageInputBox3.Text -eq "Output folder path:") {$Global:GuiHash.WizardMainPageInputBox3.Dispatcher.Invoke([action]{$Global:GuiHash.WizardMainPageInputBox3.Text = ""}); $Global:GuiHash.WizardMainPageSubmitButton2.IsEnabled= "False"}})
When I try to debug this using debug-runspace, I do not get any error on the line of code where it should disable the submit button.
Line of Code to disable the submit button:
$Global:GuiHash.WizardMainPageSubmitButton1.IsEnabled="False"
Debug Runspaces:
PS C:\Users\Administrator> Debug-Runspace Runspace11
Debugging Runspace: Runspace11
To end the debugging session type the 'Detach' command at the debugger prompt, or type 'Ctrl+C' otherwise.
Stopped at: If ($Global:GuiHash.WizardMainPageInputBox2.Text -eq "NSX Manager Admin Password:") {$Global:GuiHash.WizardMainPageInputBox2.Dispatcher.Invoke([action]{$Global:GuiHash.WizardMainPageInputBox2.Text = ""})}
[DBG]: [Process:7116]: [Runspace11]: PS C:\Users\Administrator>>
Stopped at: If ($Global:GuiHash.WizardMainPageInputBox2.Text -eq "NSX Manager Admin Password:") {$Global:GuiHash.WizardMainPageInputBox2.Dispatcher.Invoke([action]{$Global:GuiHash.WizardMainPageInputBox2.Text = ""})}
[DBG]: [Process:7116]: [Runspace11]: PS C:\Users\Administrator>>
Stopped at: $Global:GuiHash.WizardMainPageSubmitButton1.IsEnabled="False"
[DBG]: [Process:7116]: [Runspace11]: PS C:\Users\Administrator>>
Stopped at: })
[DBG]: [Process:7116]: [Runspace11]: PS C:\Users\Administrator>>
Thanks to Ed Plunkett, based on his comment I need to use the false statement $False instead of using a string. changin my code to the below works like charm. Thank you Ed.
$Global:GuiHash.WizardMainPageSubmitButton1.IsEnabled=$False
How to know the permissions of my azure ad app have for other APIs, such as Microsoft Grahp API .
In portal , i could check that in the [API Access]-->[Required permissions] , but how do i check that with powershell , i used
Get-AzureRmADApplication -ObjectId ,
Get-AzureRmADApplication -ObjectId xxxxx | fl *
But little attributes returned and AppPermissions is null , but with fiddle , i notice it use below request :
GET https://graph.windows.net/mytenant/applications/id?api-version=1.6 HTTP/1.1
And i could find a lot of attributes of that app ,which one shows the permission of the app and how do i get that in powershell ?
You could try the Azure Active Directory PowerShell Version 2 , the use command like :
$app = Get-AzureADApplication -Filter "appId eq '$appId'" | fl *
to get the RequiredResourceAccess claim ,that is the collection that is shown under "permissions to other applications" in the azure ad classic portal and "Required permissions" in new portal .
In addition , PowerShell essentially wraps the API's and just presents them to you in a simplified interface. If you don't find a command to do what you want you can always using PowerShell to invoke the Graph API directly. Please refer to below article for how to call Azure Active Directory Graph Api from Powershell :
https://blogs.technet.microsoft.com/paulomarques/2016/03/21/working-with-azure-active-directory-graph-api-from-powershell/
And here is a test code sample :
PS C:\Users\v-nany> $header = #{
>> 'Content-Type'='application\json'
>> 'Authorization'=$token.CreateAuthorizationHeader()
>> }
PS C:\Users\v-nany> $uriSAs = "https://graph.windows.net/xxxxxxx/applications/xxxxxx?api-version=1.6 "
PS C:\Users\v-nany> $appInfo = (Invoke-RestMethod -Uri $uriSAs –Headers $header –Method Get –Verbose)
PS C:\Users\v-nany> $appInfo.requiredResourceAccess
You will get resourceAppId represents the resource , and related resourceAccess which is a scope list.
I am building an app, in my app i am want to have a page for an admin to manage user menu permissions. What this means is that supposing i have the following menus:
Products
-Add
-Edit
-View
-Delete
And
Users
-Add
-Edit
-View
-Delete
Now i want User A to be only able to see and go to:
Products:
-View
And
Users:
-View
The rest of the menu items will be invisible and inaccessible to User A.
What i have set out to do is to create tables for this in my database:
menu_groups
-id
-name
-description
menu_items
-id
-menu_group_id
-name
-state
-description
Menu groups table will hold all top-level menu items, in this case: products and users.
Menu items table will hold sub menu items: eg. Add, Edit, View, Delete
Now when the app loads i plan on checking user permissions then loading these menu items from the database, displaying and then caching them. Obviously i will put a filter/middleware or whatever it's called on those routes on the server so no one can bypass it.
My question now is, is this the right way of doing it? In terms of security, efficiency etc. Or is there another simple way of achieving this?
I use mysql for my database, laravel-5 on my server and angularjs for client side.
I did something similar with my application, it's a good solution. When a user tries to access a forbidden section on my website, laravel returns a 401 error which I catch automatically in my angular application by an interceptor.
$httpProvider.interceptors.push(function() {
return {
'responseError': function(rejection) {
if(rejection.status === 401) {
//Redirect to login, show error etc.
}
};
});
I am new at PowerShell and I have been researching this issue, but I have not been able to find what I am looking for yet. The site is still using PowerShell 2, and it cannot be upgraded.
I have a list of servers that I want to ping to verify that they are up, and I am using a text file to populate the names. I have the pinging part of the script working and the emailing part working just fine. Now I am trying to combine the two to email about a specific server that fails.
I am using the following:
Test-Connection -ComputerName (Get-Content Computers.txt) -Delay 5 -Quiet
This returns the boolean values of true or false for each computer.
I know that the Get-Content automatically creates an array. So is the best way to get the results written as an associative array, or is there a way to get the Test-Connection to loop one at a time through the Computers.txt array and then just send the email on a failure when it is on that specific server?
Try this:
Get-Content Computers.txt | ForEach-Object {
if (Test-Connection -ComputerName $_ -Delay 5 -Quiet) {
#Send email that it was a success
} else {
#Computer unreachable
}
}
Basically you pipe the list of computers to a foreach-object loop, and iterate through the list, pinging each computer and sending the email.