powershell-4.0

Loading Assemblies from NuGet Packages

社会主义新天地 提交于 2019-11-28 23:31:11
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 call Add-Type -AssemblyName automatically. It'd be a lot like using npm or pip in Node.js or Python,

read section of lines from Cisco IOS configuration loaded as text file in powershell

萝らか妹 提交于 2019-11-28 14:46:52
I'm not expert in powershell but looking to write function in powershell to read section of lines from Cisco IOS configuration loaded in as text file in powershell. there will be multiple sections with different names, each section have parent line with child section as below in configuration. "interface" section have names, "object" section have names and "object-group" section have names to filter them or search. so how to write function to get each section of lines and than parse further to get IPs from that section. IOS Configuration Example: interface GigabitEthernet0/0 description XXX

Select option from Array

对着背影说爱祢 提交于 2019-11-28 14:27:42
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 { param ( [Parameter(Mandatory = $true)] $ComputerName, [System.Management.Automation.Credential()]

How to unzip a Zip File with Powershell Version 2.0?

谁都会走 提交于 2019-11-28 07:41:15
问题 This works for me with PowerShell version 4.0 or higher. But at PowerShell version 2.0 the Add-Type isn't possible (type doesn't exist). function unzip { Add-Type -Assembly “system.io.compression.filesystem” [io.compression.zipfile]::ExtractToDirectory("SOURCEPATH\ZIPNAME", "DESTINATIONPATH") } 回答1: function Expand-ZIPFile($file, $destination) { $shell = new-object -com shell.application $zip = $shell.NameSpace($file) foreach($item in $zip.items()) { $shell.Namespace($destination).copyhere(

Powershell - Regular Expression Multiple Matches

六月ゝ 毕业季﹏ 提交于 2019-11-28 04:34:09
问题 Maybe my reasoning is faulty, but I can't get this working. Here's my regex: (Device\s#\d(\n.*)*?(?=\n\s*Device\s#|\Z)) Try it: http://regex101.com/r/jQ6uC8/6 $getdevice is the input string. I'm getting this string from the Stream/Output from a command line tool. $dstate = $getdevice | select-string -pattern '(Device\s#\d(\n.*)*?(?=\n\s*SSD\s+|\Z))' -AllMatches | % { $_ -match '(Device\s#\d(\n.*)*?(?=\n\s*SSD\s+|\Z))' > $null; $matches[0] } Write-Host $dstate Output: Device #0 Device #1

Determine if script is running hidden

我们两清 提交于 2019-11-27 22:51:45
问题 I am trying to programmatically determine if a .ps1 script is running visibly or not. If it is running visibly, it should restart itself hidden. If it is already hidden, take no action. The problem I have is a loop where it continually restarts itself because hidden status cannot be determined. I've been looking at both get-process cmdlet and GWMI Win32_process and see nothing like a .visible property to check status. If ($me -eq visible ???) { $Invisible = New-Object System.Diagnostics

read section of lines from Cisco IOS configuration loaded as text file in powershell

丶灬走出姿态 提交于 2019-11-27 08:42:54
问题 I'm not expert in powershell but looking to write function in powershell to read section of lines from Cisco IOS configuration loaded in as text file in powershell. there will be multiple sections with different names, each section have parent line with child section as below in configuration. "interface" section have names, "object" section have names and "object-group" section have names to filter them or search. so how to write function to get each section of lines and than parse further

Block of code prevents script from running, but runs interactively

南楼画角 提交于 2019-11-27 07:57:54
问题 I have a script that is run as a scheduled task which fails with an unexpected token error on on the line where $As is defined. If I remove the code, the script runs properly. If I paste the whole script (including the problematic section) into a PowerShell window everything runs as expected. I'm assuming this is a simple gotcha that I've just not encountered, but I cannot figure out what the problem is with it, more experienced eyes would be appreciated. This is being run on Server 2012R2,

Using Get-ChildItem -Exclude or -Include returns nothing

南楼画角 提交于 2019-11-27 04:41:06
问题 I have a list in my C: directory that has many files. If I try to run an -Exclude on it, I get no returns. Same with -Include. If I use filter it returns what I expected to get back. Am I not understanding what it should be doing? Here is an example of what I am running and getting nothing: Get-ChildItem -Path C: -Exclude "*.txt" I get nothing back. If I run Get-Childitem -filter "*.txt" I get this back: Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 11/7/2007 8:00 AM

rename-item and override if filename exists

余生长醉 提交于 2019-11-26 23:26:25
问题 I am trying to use the Rename-Item cmdlet to do the following: I have folder which contains " *.txt " files. Let's say 1.txt, 2.txt etc... I want to copy files that have some prefix, rename them to *.txt and override any existing files if there are any. example: folder: c:\TEST files : 1.txt, 2.txt, 3.txt, 4.txt, 5.txt, index.1.txt, index.2.txt, index.3.txt, index.4.txt, index.5.txt I want to use rename-item to filter any index.*.txt and rename them to *.txt , and if the same file exists,