powershell

Difference between Clear-Variable and setting variable to NULL

点点圈 提交于 2021-02-07 13:17:57
问题 I often use variables which are declared in the script scope to avoid problems with functions and their scopes. I am declaring these variables like this: New-Variable -Name test -Option AllScope -Value $null ... or sometimes I switch existing variables like this to use them comprehensively: $script:test = $test When I want to clear them I either use this: Clear-Variable test -Scope Script ... or I simply use this: $test = $null Is there a difference? What should I prefer and why? 回答1: From

Difference between Clear-Variable and setting variable to NULL

北战南征 提交于 2021-02-07 13:17:33
问题 I often use variables which are declared in the script scope to avoid problems with functions and their scopes. I am declaring these variables like this: New-Variable -Name test -Option AllScope -Value $null ... or sometimes I switch existing variables like this to use them comprehensively: $script:test = $test When I want to clear them I either use this: Clear-Variable test -Scope Script ... or I simply use this: $test = $null Is there a difference? What should I prefer and why? 回答1: From

Powershell Invoke-RestMethod incorrect character

余生长醉 提交于 2021-02-07 12:53:20
问题 I'm using Invoke-RestMethod to get page names from an application I'm using. I notice that when I do a GET on the page it returns the page name like so This page â is working However the actual page name is This page – is working Here's how my request looks Invoke-WebRequest -Uri ("https://example.com/rest/api/content/123789") -Method Get -Headers $Credentials -ContentType "application/json; charset=utf-8" The problem is with the en-dash, does anyone know how I can fix this? 回答1: In case of

PowerShell - execute script block in specific scope

余生长醉 提交于 2021-02-07 12:12:04
问题 I am trying to implement RSpec/Jasmine like BDD framework in Powershell (or at least research the potential problems with making one). Currently I am having problems with implementing simple before/after functionality. Given $ErrorActionPreference = "Stop" function describe() { $aaaa = 0; before { $aaaa = 2; }; after { $aaaa; } } function before( [scriptblock]$sb ) { & $sb } function after( $sb ) { & $sb } describe the output is 0, but I would like it to be 2. Is there any way to achieve it

PowerShell - execute script block in specific scope

一世执手 提交于 2021-02-07 12:10:55
问题 I am trying to implement RSpec/Jasmine like BDD framework in Powershell (or at least research the potential problems with making one). Currently I am having problems with implementing simple before/after functionality. Given $ErrorActionPreference = "Stop" function describe() { $aaaa = 0; before { $aaaa = 2; }; after { $aaaa; } } function before( [scriptblock]$sb ) { & $sb } function after( $sb ) { & $sb } describe the output is 0, but I would like it to be 2. Is there any way to achieve it

Unicode support for Invoke-Sqlcmd in PowerShell

别等时光非礼了梦想. 提交于 2021-02-07 11:58:37
问题 The PowerShell sqlps module provides core support for SQL Server access from within PowerShell and its Invoke-Sqlcmd cmdlet is its main workhorse for executing literal queries or SQL script files (analogous to the non-PowerShell sqlcmd utility). I recently tried some experiments to confirm that Invoke-Sqlcmd handles Unicode and had some surprising results. I started with this simple script file (named unicode.sql): CREATE TABLE #customers ( [IdCust] int, [FirstName] nvarchar(25), [SurName]

PowerShell Add-WindowsFeature unrecognized

时光总嘲笑我的痴心妄想 提交于 2021-02-07 11:16:17
问题 Thanks first of all for reviewing this. I've basically got a third-party agent software which allows me to execute PowerShell as LocalSystem. This allows me to easily run remote PowerShell commands without WinRM etc. The problem that i'm running into is that on some servers i'm not able to perform get-WindowsFeature or Add-WindowsFeature. An example of how i'm trying to achieve this is here: Import-Module ServerManager; Get-WindowsFeature; The output is as such: The term 'Get-WindowsFeature'

PowerShell Add-WindowsFeature unrecognized

只谈情不闲聊 提交于 2021-02-07 11:14:43
问题 Thanks first of all for reviewing this. I've basically got a third-party agent software which allows me to execute PowerShell as LocalSystem. This allows me to easily run remote PowerShell commands without WinRM etc. The problem that i'm running into is that on some servers i'm not able to perform get-WindowsFeature or Add-WindowsFeature. An example of how i'm trying to achieve this is here: Import-Module ServerManager; Get-WindowsFeature; The output is as such: The term 'Get-WindowsFeature'

Powershell script run from task scheduler unable to open word document

那年仲夏 提交于 2021-02-07 10:35:21
问题 I am trying to use a powershell script to get the wordcount from a number of word files and then output that to a csv file. This works as expected when run from the powershell prompt, and works when called from the cmd prompt directly or from inside a perl script, however the script fails to work when called as a scheduled task. This is not an ExecutionPolicy issue causing the script to not run at all. The script is running and produces some output, but when launched from task scheduler it is

speed up powershell's remove-netfirewallrule

巧了我就是萌 提交于 2021-02-07 10:30:58
问题 I need to remove a lot of metro app related firewall rules in Windows 10 with powershell. It seems very slow compared to netsh or regedit. Anyway to speed it up? # clean firewall rules, deleting profile doesn't get rid of them # string (sid) 45 in length, no existing profiles # 9000 rules take about 90 minutes to delete $profiles = get-wmiobject -class win32_userprofile # I'm only dumping to a file to convert pscustomobject to string for sort get-netfirewallrule -all | select-object -property