Power Shell
16 TopicsHow to add a new set of key & values in json file.
Problem Statement: I want to add a set of new key & value in existing parsed Json after specific index. or position. For example: I have imported Json in $Json variable and then I wanted to add new sets of property right after property name 'Service1'. I able to make the script working out for me. However, not able to add new sets of key & value or property after specific position in file. PowerShell Code: function Get-EnvironmentManifest([string]$Filename) { $Settings = Get-Content -Path $Filename -Encoding UTF8 | ConvertFrom-Json -ErrorAction STOP return $Settings } if (([System.Net.Dns]::GetHostByName($env:computerName)).HostName.Split('.')[0] -notmatch "UAT*") { $EnvironmentString = (([System.Net.Dns]::GetHostByName($env:computerName)).HostName.Split('.')[0].Split('-')[1]).ToUpper() for ($i = 1; $i -lt 5; $i++) { $ServiceName = 'Service' + [char](65 + $i) $HastTable = [ordered] @{"Name" = "Service1"; "ProfileType" = "Windows"; "ServiceName" = "$ServiceName";} $Settings = Get-EnvironmentManifest -Filename $TargetJSON $Asset = New-Object -TypeName PSObject $Asset | Add-Member -NotePropertyMembers $HastTable -TypeName 'Asset' $Settings.Profiles.Services += $Asset $Settings | ConvertTo-Json -Depth 3| Set-Content -Path $TargetJSON } } I Was Able to Create Following JSON Using Above PowerShell Code: { "Environment": {}, "Profiles": { "Services": [ { "Name": "A" }, { "Name": "B" }, { "Name": "C" }, { "Name": "D" }, { "Name": "Service1", "ProfileType": "Windows", "ServiceName": "ServiceA" }, { "Name": "E" }, { "Name": "F" }, { "Name": "Service1", "ProfileType": "Windows", "ServiceName": "ServiceB" }, { "Name": "Service1", "ProfileType": "Windows", "ServiceName": "ServiceC" }, { "Name": "Service1", "ProfileType": "Windows", "ServiceName": "ServiceD" }, { "Name": "Service1", "ProfileType": "Windows", "ServiceName": "ServiceE" } ] } } But I Want to Have Following Json: { "Environment": {}, "Profiles": { "Services": [ { "Name": "A" }, { "Name": "B" }, { "Name": "C" }, { "Name": "D" }, { "Name": "Service1", "ProfileType": "Windows", "ServiceName": "ServiceA" }, { "Name": "Service1", "ProfileType": "Windows", "ServiceName": "ServiceB" }, { "Name": "Service1", "ProfileType": "Windows", "ServiceName": "ServiceC" }, { "Name": "Service1", "ProfileType": "Windows", "ServiceName": "ServiceD" }, { "Name": "Service1", "ProfileType": "Windows", "ServiceName": "ServiceE" }, { "Name": "E" }, { "Name": "F" } ] } }Solved30KViews0likes3Comments[System.Drawing.Bitmap] not found in powershell,but at powershell ise is exist!
hello everyone: I have a question, such as a title. - - - - - - - - - - - - - - -powershell - - - - - - - - - - ---------------------- PS C:\> [System.Drawing.Bitmap] [System.Drawing.Bitmap] CategoryInfo:InvalidOperation:(System.Drawing.Bitmap:TypeName)[],RuntimeException FullyQualifiedErrorId:TypeNotFound ----------------------------- powershell ise ------------------- ---------------------- PS C:\> [System.Drawing.Bitmap] IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Bitmap System.Drawing.Image ------------------------------version------------------------------------------------ Name Value ---- ----- PSVersion 5.1.17763.771 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.17763.771 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 PS:Powershell is the same version as powershell ise11KViews0likes2CommentsCreating Powerplans
Hello, I want to create a script using Powershell to set a default power plan. I already found something for the good old CMD but nothing yet for Powershell, so maybe you guys can help?! The Settings: Powerbutton: When I press Powerbutton: Shutdown - Shutdown When I press Sleepbutton: Nothing - Nothing When i close the lid: -Nothing -nothing Plan-Settings: Turn off Display: 15min - never Put it to sleep: Never - Never I would appreciate if you could give me some advice on how to use Powershell creating "Power-Plans". Greetings Yannik SchulzSolved8.6KViews1like7CommentsBasic ssh works but X11 does not
I have the basic features of <ssh remotehostname> working right now. By "working" I mean that I can successfully connect to another machine (Linux machine) through an ssh session in powershell using a command like <ssh remotehostname>. I've already setup my /.ssh/config file and public keys which works quite nicely. However, I cannot get X11 to work. Any time I try to start this GUI application (which happens to be the eclipse IDE on this linux machine) it gives the following error (PowerShellError.png). I've went ahead and setup cygwin64 and it's working like a charm. I have X11 working just fine and can launch this same GUI application just fine. I'm attaching this output as well (Cygwin64.png). What is going on here? How can I troubleshoot this error. I don't believe this missing package called "cairo" is the issue since it happens on cygwin as well. I also get this missing "cairo" package error even when I'm directly working from this linux machine. I think the issue here is related to some display setting or powershell setting that is not being applied??? I do not know much about powershell so the more detailed you are is much appreciated.8.5KViews0likes5CommentsWarning PowerShell ID 300
HI. Warning PowerShell ID 300 - Device not ready occurs at every start or starting W.P.S 5.1. - <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"> - <System> <Provider Name="PowerShell" /> <EventID Qualifiers="0">300</EventID> <Version>0</Version> <Level>3</Level> <Task>3</Task> <Opcode>0</Opcode> <Keywords>0x80000000000000</Keywords> <TimeCreated SystemTime="2023-02-09T13:24:44.9356909Z" /> <EventRecordID>657</EventRecordID> <Correlation /> <Execution ProcessID="7524" ThreadID="0" /> <Channel>Windows PowerShell</Channel> <Computer>Windows-11</Computer> <Security /> </System> - <EventData> <Data>Dispositivo non pronto.</Data> <Data>ProviderName=Microsoft.PowerShell.Core\FileSystem ExceptionClass=IOException ErrorCategory= ErrorId= ErrorMessage=Dispositivo non pronto. Severity=Warning SequenceNumber=13 HostName=ConsoleHost HostVersion=5.1.22621.963 HostId=f904787d-5901-4255-b784-310a76225a8b HostApplication=powershell.exe -ExecutionPolicy Restricted -Command Write-Host 'Final result: 1'; EngineVersion= RunspaceId= PipelineId= CommandName= CommandType= ScriptName= CommandPath= CommandLine=</Data> </EventData> </Event> and the following warning appears: And if I open the link I get version 7 which I already have installed. Is there a solution to avoid this warning? Thank youSolved5.4KViews0likes13CommentsUnable to invoke jupyter notebook in powershell (Unable to create process) (No such file/directory)
I have two installs of python and have added the following to my user path environment variable... C:\Python\Python36\ C:\Python\Python36\Scripts C:\Python\Python37 C:\Python\Python37\Scripts I have installed jupyter notebook to the following location... C:\Python\Python37\Scripts\ You can clearly see that jupyter-notebook.exe is indeed there. However, no matter what command I use I can't start it. If I navigate to the directory where the exe is I can start the jupyter-notebook.exe just fine. Therefore, why can't I invoke "python37 jupyter-notebook.exe" from my home directory or anywhere other than the location it resides? It's in my user path environment variable so I'm lost on what else to try.4.8KViews0likes0CommentsGetting all virtual machines in a Hyper-V cluster using Python and WinRM
Hello, I am trying to use Python and WinRM to retrieve a list of all virtual machines in a Hyper-V cluster. I have a PowerShell script that works to retrieve the virtual machines owned by the current node, but I am having trouble modifying it to retrieve all virtual machines in the cluster. Here's the current script that retrieves the virtual machines owned by the current node: # Create a PowerShell session on the host machine session = winrm.Session(host, auth=(username, password),transport='ntlm') # Define the PowerShell command to retrieve the list of virtual machines in the cluster ps_script = """ $nodes = Get-ClusterNode Write-Output $nodes $vm_list = Get-ClusterGroup -Cluster $env:computername | Where-Object {$_.GroupType -eq 'VirtualMachine' -and $_.OwnerNode.Name -in $nodes.Name} | Get-VM $vm_names = $vm_list.Name Write-Output $vm_names """ # Execute the PowerShell command and retrieve the output result = session.run_ps(ps_script) if result.status_code == 0: # Parse the output to get the list of virtual machines vm_info = result.std_out.decode('utf-8').strip() # Print the list of virtual machines print(vm_info) else: # Print the full error message print("Error message: " + result.std_err.decode('utf-8').strip()) Can anyone help me modify this script to retrieve all virtual machines in the cluster, regardless of which node owns them? Thank you in advance for your help!2.3KViews0likes2Commentspowershell create a catalog file
Hi all, I am new to powershell. I am looking for powershell script to generate a catalog file which display all the subfolders and files underneath, better to comment out the subfolders, and only files listed, but should be listed just under the sunfolders. let say I have a folder called total_folder underneath there are many subfolders folder1 file1-1 fille1-2 folder2 file2-1 file2-2 file2-3 folder3 file3-1 ...... Now I would like to document these info into a catalog file as below ;----folder1---- file1-1 fille1-2 ;----folder2---- file2-1 file2-2 file2-3 ;----folder3---- file3-1 ......Solved1.9KViews0likes2CommentsAutomating Daily Task
Hello, I am new to PowerShell and want to try using it to semi-automate a repetitive task. Any help would be greatly appreciated. I need to open multiple websites (10+) in IE (not updated yet), they also all have acknowledgement popups. They need to open in individual tabs. I am trying to set up an easy way for someone to verify the sites are up and running daily. 1. What I would like to do is have PowerShell open each site from a text file (list) in its own tab. 2. In the same session. 3. Automatically click the OK on the acknowledgement popup. 4. If the site fails to open display the error in the tab. And if this is not possible can a GUI be created in PowerShell, that lets you select each site, open them in the same session on a different tab? I am trying to do this so anyone can perform this task.1.5KViews0likes3Comments