powershell-3.0

Administrative privileges

穿精又带淫゛_ 提交于 2019-12-12 22:40:45
问题 In a thread here I found a way to check whether a user has administrative privileges. However, when I try to use boolean logic on this it fails to work. $user = [Security.Principal.WindowsIdentity]::GetCurrent(); (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) if($user = $false){cls;Write-warning "Starting and stopping services can only be done with administrative privileges.`nPlease restart this script from an elevated

Move a files that have the same name but different extension. Powershell

霸气de小男生 提交于 2019-12-12 18:23:56
问题 I am a junior tech and have been tasked to write a short powershell script. The problem is that I have started to learn the PS 5 hours ago - once my boss told that I'm assigned to this task. I'm a bit worried it won't be completed for tomorrow so hope you guys can help me a bit. The task is: I need to move the files to different folders depending on certain conditions, let me start from the he folder structure: c:\LostFiles: This folder includes a long list of .mov, .jpg and .png files c:

Real Time Data With Powershell GUI

喜欢而已 提交于 2019-12-12 17:38:46
问题 I'm struggling here. Using Powershell and GUI, how to automatically refresh data on a form? Example with the script below, how to automatically update the label with the number of process executed by my computer? Add-Type -AssemblyName System.Windows.Forms $Form = New-Object system.Windows.Forms.Form $Form.Text = "Sample Form" $Label = New-Object System.Windows.Forms.Label $Label.Text = "Number of process executed on my computer" $Label.AutoSize = $True $Form.Controls.Add($Label) $Form

Permanently add PowerShell module? (Import-Module)

梦想与她 提交于 2019-12-12 17:25:22
问题 I'm using a third-party module I found on GitHub and importing it with: powershell Import-Module .\foo.ps1 This imports successfully and the module works great. But when I open a new PowerShell terminal, it doesn't load the module, I have to run the Import-Module command everytime. Is it possible to have modules load permanently? I'm using Windows 10 with PowerShell v3. 回答1: You can add it to one of your profiles that powershell loads by default. Best bet is %UserProfile%\My Documents

Powershell - Create new line for multiple array objects using Export-csv

偶尔善良 提交于 2019-12-12 16:41:01
问题 I have an odd one that I haven't seen much writing on. Anyway, here goes. I'm trying to build an array and export it to CSV. The problem is, if there is more than one result returned, I can't figure out how to add it to the CSV as a new line. I am currently using a -join to throw all the results into the same cell, but that's not optimal. What I'd really like to do is add a new row and throw the extra results underneath it in the same column. Does that make sense? Here's what I have now: #

How to check if file has valid JSON syntax in Powershell

送分小仙女□ 提交于 2019-12-12 10:35:03
问题 I am trying to write a powershell script that reads a file and prints "true" if it is a valid JSON file. I am using Powershell v3.0 and this is what I have right now : $text = Get-Content .\filename.txt -Raw $powershellRepresentation = $text | ConvertFrom-Json How do I check the return code? I mean I want something like this : if(file not a JSON file){ Write-Host "not JSON" } else{ Write-Host "True" } 回答1: There isn't a Test-Json like cmdlet, so the best way is to put your ConvertFrom-Json

Decoding base64 with powershell

自作多情 提交于 2019-12-12 09:33:51
问题 I'm attempting to decode an attachment in an email file from base64 and save it to disk. For testing purposes, this is my code. Where input.txt contains just the base64 encoded data, which is an HTML file. $file = "C:\input.txt" $data = Get-Content $file [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($data)) > out.html The decoding works fine, and it generates a new file that contains all of the lines, and is visibly identical to the original attachment. The

How can I use PowerShell's -filter parameter to exclude values/files?

会有一股神秘感。 提交于 2019-12-12 08:42:29
问题 I'm currently running a PowerShell (v3.0) script, one step of which is to retrieve all the HTML files in a directory. That works great: $srcfiles = Get-ChildItem $srcPath -filter "*.htm*" However, now I'm faced with having to identify all the non-HTML files...CSS, Word and Excel docs, pictures, etc. I want something that would work like the -ne parameter in conjunction with the -filter parameter. In essence, give me everything that's not "*.htm*" -filter -ne doesn't work, I tried -!filter on

Use cases of [ordered], the new PowerShell 3.0 feature

倾然丶 夕夏残阳落幕 提交于 2019-12-12 07:46:43
问题 PowerShell 3.0 CTP1 introduces a new feature [ordered] which is somewhat a shortcut for OrderedDictionary . I cannot imagine practical use cases of it. Why is this feature really useful? Can somebody provide some useful examples? Example: this is, IMHO, rather demo case than practical: $a = [ordered]@{a=1;b=2;d=3;c=4} (I do not mind if it is still useful, then I am just looking for other useful cases). I am not looking for use cases of OrderedDictionary , it is useful, indeed. But we can use

How can you test if an object has a specific property?

空扰寡人 提交于 2019-12-12 07:09:57
问题 How can you test if an object has a specific property? Appreciate I can do ... $members = Get-Member -InputObject $myobject and then foreach through the $members , but is there a function to test if the object has a specific property? Additional Info: The issue is I'm importing two different sorts of CSV file, one with two columns, the other with three. I couldn't get the check to work with "Property", only with "NoteProperty" ... whatever the difference is if ( ($member.MemberType -eq