powershell-4.0

How can I query a temporary PS-Drive while returning files with a name relative to the drive?

我的未来我决定 提交于 2019-12-18 07:36:30
问题 I am trying to create a script where I will be searching the file servers for non inherited permissions. I have run into the 260 character limit for file names as a result. A suggestion I saw, that I thought would help, a couple of times was to create some non persistent PS Drives a couple of levels deep and query those. Problem is when I use Get-ChildItem against the new PS Drives it is returning object with the full network path and not using the name I assigned it. # Cycle the folders Get

Invoke-Command faster than the command itself?

China☆狼群 提交于 2019-12-18 04:16:30
问题 I was trying to measure some ways to write to files in PowerShell. No question about that but I don't understand why the first Measure-Command statement below takes longer to be executed than the 2nd statement. They are the same but in the second one I write a scriptblock to send to Invoke-Command and in the 1st one I only run the command. All informations about Invoke-Command speed I can find are about remoting. This block takes about 4 seconds: Measure-Command { $stream = [System.IO

Error Handling for Invoke-RestMethod - Powershell

前提是你 提交于 2019-12-18 03:04:18
问题 I have a powershell script using the Skytap API (REST). I would like to catch the error, if there is one, and try to display it. For example, we are changing the IP: Invoke-RestMethod -Uri https://cloud.skytap.com/configurations/XXXXXX/vms/YYYYYY/interfaces/ZZZZZZ?ip=10.0.0.1 -Method PUT -Headers $headers If the IP is used somewhere else, I will get the 409 Conflict Error (Request is well-formed but conflicts with another resource or permission). I would like to check if the error is 409 and

Loading Assemblies from NuGet Packages

試著忘記壹切 提交于 2019-12-17 23:29:02
问题 Sometimes in my PowerShell scripts, I need access to a specific DLL, using Add-Type -AssemblyName . However, the DLLs I need aren't always on the machine or in the GAC. For instance, I might want a quick script that uses Dapper to query a database. In these cases, I have been literally copying the DLLs along with the ps1 file. I was wondering if this was common/a good idea and whether there was an existing extension that would load up NuGet packages, store then in a global or local folder and

Select option from Array

孤街浪徒 提交于 2019-12-17 21:14:32
问题 I am working on a side project and to make it easier for managment since almost all of out server names are 15 charactors long I started to look for an RDP managment option but none that I liked; so I started to write one and I am down to only one issue, what do I do to manage if the user types not enough for a search so two servers will match the Query. I think I will have to put it in an array and then let them select the server they meant. Here is what I have so far function Connect-RDP {

How to search a word in a file using PowerShell script

大城市里の小女人 提交于 2019-12-13 20:05:13
问题 I have a list of 350 folders and each folder has a file Access log. I need to search all 350 files under all 350 folders for a name "Hound" and display the name of the folders which contain the name "Hound" in their access log file. Below is my code, can someone help me with what should be added here to get the desired output, please? #List all the folders in C:\testfolder $folders = (Get-ChildItem -Path "C:\testfolder" | Where-Object{$_.Attributes -eq "Directory"} | Select Fullname) #looping

Only pass parameter if value supplied

霸气de小男生 提交于 2019-12-13 15:13:53
问题 Background I've created a wrapper function around Write-EventLog so that I can easily call it without having to concern myself with checking for & creating an event log/source each time. By simply adding a -Force parameter, I want it to create the log if it doesn't exist; if I don't add that parameter it should behave as normal (which reduces overhead of multiple checks if called from code where I know that log will exist). I've copied the list of parameters available to write-eventlog such

how to count rows in a csv and then loop through the contents

笑着哭i 提交于 2019-12-13 09:31:14
问题 I have a csv that i want to check the count of rows and then loop through the contents. Im using the code at the bottom to get the count which works but im not sure how i can loop through the csv and get the values in each column. Ive read that i can do it using the select-object cmdlet if i specify the column names however this code will work on a number of csv's all with different column names. Any ideas how i can make this work? $csv = Import-Csv -Path $requestFile | measure if(($csv)

if script runs true restart/run next part

Deadly 提交于 2019-12-13 08:59:19
问题 I'm trying to get the script to restart the computer when the script returns $true, I'm planning to run the script in SCCM or task scheduler. I was going to get the SCCM to restart the computer if it returns $true but don't know how to so now I'm trying to add a restart within the script itself but don't know how to add it correctly . function Test-PendingReboot { if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) { return

Using two Where commands to run two checks

谁说胖子不能爱 提交于 2019-12-13 08:35:53
问题 I am using this command Where {$_.Extension -match "zip||rar"} but need to use this command too Where {$_.FullName -notlike $IgnoreDirectories} Should I use a && or II as in Where {$_.Extension -match "zip||rar"} && Where {$_.FullName -notlike $IgnoreDirectories} or Where {$_.Extension -match "zip||rar"} || Where {$_.FullName -notlike $IgnoreDirectories} ? What I am trying to accomplish is to have every zip and rar file be extracted but I want to skip the extraction of the zip or rar files in