powershell

A better way to slice an array (or a list) in powershell

可紊 提交于 2021-02-08 07:22:32
问题 How can i export mails adresses in CSV files in a range of 30 users for each one . I have already try this $users = Get-ADUser -Filter * -Properties Mail $nbCsv = [int][Math]::Ceiling($users.Count/30) For($i=0; $i -le $nbCsv; $i++){ $arr=@() For($j=(0*$i);$j -le ($i + 30);$j++){ $arr+=$users[$j] } $arr|Export-Csv -Path ($PSScriptRoot + "\ASSFAM" + ("{0:d2}" -f ([int]$i)) + ".csv") -Delimiter ";" -Encoding UTF8 -NoTypeInformation } It works but, i think there is a better way to achieve this

“$xyz” and “Write-Host ”$xyz“” giving different output

巧了我就是萌 提交于 2021-02-08 07:22:19
问题 I am hashing all the files in one location, an origin folder, and writing the hashes to a variable and then doing the same to all the files in another location, a destination folder: $origin = Get-ChildItem .\Test1 | Get-FileHash | Format-Table -Property Hash -HideTableHeaders $destination = Get-ChildItem .\Test2 | Get-FileHash | Format-Table -Property Hash -HideTableHeaders Then I am comparing them with Compare-Object like so: Compare-Object $origin $destination Now in my test I purposefully

How to insert list as parameter from powershell to SqlPlus

戏子无情 提交于 2021-02-08 07:22:11
问题 I have a query which is like this in a file : script.sql select * from test_table where name in (&1) This is an example of query i'm trying to make and i execute it with a power shell script which is like this : $names = '''Name1'', ''Name2''' $params = '"' + $names + '"' sqlplus -silent $username/$password@$tnsalias "@script.sql" $params Note that $names has a variable list of names But then when the script is executed, the parameter get substitute like this : former : where name in (&1) new

“$xyz” and “Write-Host ”$xyz“” giving different output

亡梦爱人 提交于 2021-02-08 07:21:56
问题 I am hashing all the files in one location, an origin folder, and writing the hashes to a variable and then doing the same to all the files in another location, a destination folder: $origin = Get-ChildItem .\Test1 | Get-FileHash | Format-Table -Property Hash -HideTableHeaders $destination = Get-ChildItem .\Test2 | Get-FileHash | Format-Table -Property Hash -HideTableHeaders Then I am comparing them with Compare-Object like so: Compare-Object $origin $destination Now in my test I purposefully

“$xyz” and “Write-Host ”$xyz“” giving different output

眉间皱痕 提交于 2021-02-08 07:21:02
问题 I am hashing all the files in one location, an origin folder, and writing the hashes to a variable and then doing the same to all the files in another location, a destination folder: $origin = Get-ChildItem .\Test1 | Get-FileHash | Format-Table -Property Hash -HideTableHeaders $destination = Get-ChildItem .\Test2 | Get-FileHash | Format-Table -Property Hash -HideTableHeaders Then I am comparing them with Compare-Object like so: Compare-Object $origin $destination Now in my test I purposefully

Send string parameters to a Start-Job script block

别来无恙 提交于 2021-02-08 06:53:25
问题 I need to initialize a job using the shell. The job will be a delay plus a call to a vbScript. The following code works fine. For my example, the vbScript is just a single line with a MsgBox "Hello world!" $functions = { Function execute_vbs { param ([string]$path_VBScript, [int]$secs) Start-Sleep -Seconds $secs cscript /nologo $path_VBScript } } $seconds = 2 Start-Job -InitializationScript $functions -ScriptBlock {execute_vbs -path_VBScript 'C:\Users\[USERNAME]\Desktop\hello_world.vbs' -secs

How do I pass a class object in a argument list to a another computer and call a function on it?

筅森魡賤 提交于 2021-02-08 06:39:38
问题 I am attempting to create a class object and use Invoke-Command to call a function on the class on a remote machine. When I use Invoke-Command with no computer name this works fine but when I attempt to do this on a remote computer I get an error saying the that the type does not contain my method. Here is the script I am using for testing this. $ComputerName = "<computer name>" [TestClass]$obj = [TestClass]::new("1", "2") Get-Member -InputObject $obj $credentials = Get-Credential Invoke

Powershell for Matching and Replacing Partially Matching Patterns

被刻印的时光 ゝ 提交于 2021-02-08 06:39:38
问题 Been going crazy all week unable to solve this issue. I have a dictionary word file that will be a few million words at one point, for now let's assume it's just a text file "Words.txt" which has: App Apple Application Bar Bat Batter Cap Capital Candy What I need it to do is to match each string against the rest of the file and only write output of the first hit. This will be alphabetical. Example the desired output from the words above would be: App - due to pattern "App" being seen first

Self Delete Folder with scripts powershell

非 Y 不嫁゛ 提交于 2021-02-08 06:38:13
问题 I have been trying to have powershell or batch scripts delete a folder that contains all my scripts when I am finished. Originally I tried Remove-Item -Path "C:\Tool" -Recurse -Force which worked no problem if run as a script for a location outside of C:\Tool . It would complain the files were in use when run from a script within. After some research I found &cmd.exe /c rd /s /q "C:\Tool" which worked much better, but even though I closed the GUI, the command wouldn't delete the img files

Self Delete Folder with scripts powershell

我的梦境 提交于 2021-02-08 06:36:44
问题 I have been trying to have powershell or batch scripts delete a folder that contains all my scripts when I am finished. Originally I tried Remove-Item -Path "C:\Tool" -Recurse -Force which worked no problem if run as a script for a location outside of C:\Tool . It would complain the files were in use when run from a script within. After some research I found &cmd.exe /c rd /s /q "C:\Tool" which worked much better, but even though I closed the GUI, the command wouldn't delete the img files