powershell-2.0

How to run interactive commands in another application window from powershell

旧城冷巷雨未停 提交于 2019-11-30 09:35:53
问题 I have another command line program which I invoke from my powershell script and would like to run some interactive commands in that window once it is opened from power shell. In other words - I do a Invoke-Item $link_to_app which opens up the interactive command line for that application and now I would like to use the application specific commands from within powershell scripts. e.g. app.exe -help to invoke the help command of the app.exe . Any pointers would help. Thanks! 回答1: Try this:

How to dump the foreach loop output into a file in PowerShell?

白昼怎懂夜的黑 提交于 2019-11-30 09:17:32
问题 I have wrote the following script to read the CSV file to perform the custom format of output. Script is below: $Content = Import-Csv Alert.csv foreach ($Data in $Content) { $First = $Data.DisplayName $Second = $Data.ComputerName $Third = $Data.Description $Four = $Data.Name $Five = $Data.ModifiedBy $Six = $Data.State $Seven = $Data.Sev $Eight = $Data.Id $Nine = $Data.Time Write-Host "START;" Write-Host "my_object="`'$First`'`; Write-Host "my_host="`'$Second`'`; Write-Host "my_long_msg="`'

Keep x number of files and delete all others - Powershell

僤鯓⒐⒋嵵緔 提交于 2019-11-30 08:25:54
I am trying to write a script that will look through a set of folders and keep only the last 10 files. The files in each folder could be created daily, weekly or monthly. I need the script to keep the 10 most recent copies regardless of the creation date or modified date. Using another post I created the script below that works but it doesnt keep 10 copies it keeps any file that isn't older than 10 days. $ftppath = "C:\Reports" Get-ChildItem $ftppath -recurse *_Report_*.zip -force|where {$_.lastwritetime -lt (get-date).adddays(-10)} |Remove-Item -force Any idea on how I can tweak this to work?

How to fetch an attribute value from xml using powershell?

谁说我不能喝 提交于 2019-11-30 08:21:53
I have a list of XML files, from which I have to get the string after a particular line. In the files, I need to look for a tag Event and get the attribute value DLLRoutine . e.g. the tag would look something like below ... <Event Definition="Validate" DLLPath="" DLLName="Helper.dll" DLLClass="HelpMain" DLLRoutine="pgFeatureInfoOnValidate_WriteToRegSelectedFeatures" InputParameters="pTreeViewFeatureTreeServerOS" RunOnce="no"/> I just need to get Dllroutine values. How to do it using PowerShell? Assuming your XML structure is something similar to: $xml = [xml]' <Events> <Event Definition=

How can I use powershell's read-host function to accept a password for an external service?

邮差的信 提交于 2019-11-30 08:16:19
I have a script I'm writing that makes a connection to a SOAP service. After the connection is made, I need to pass in a the username/pass with every command I send. The problem I have is that when I use read-host to do this, my password is shown in cleartext and remains in the shell: PS C:\Users\Egr> Read-Host "Enter Pass" Enter Pass: MyPassword MyPassword If I hide it with -AsSecureString, the value can no longer be passed to the service because it is now a System.Security.SecureString object: PS C:\Users\gross> Read-Host "Enter Pass" -AsSecureString Enter Pass: ********** System.Security

Import-Module : The specified module 'activedirectory' was not loaded because no valid module file was found in any module directory

别等时光非礼了梦想. 提交于 2019-11-30 07:50:40
问题 I am having trouble doing an import-module ActiveDirectory on a Server 2008 SP2 (64 bit). NET Framework 3.5 SP1 is installed I download the Windows6.0-KB968934-x86.msu (for ADWS) This file did not install saying that "The update does not apply to my system" Doing some research (http://anti-american.rssing.com/chan-2091246/all_p15.html) I installed hotfix in KB article 969166 and the above update installed. After a reboot, I noticed that in services, Active Directory Web Services is running I

Convert PowerShell script into non-readable format

情到浓时终转凉″ 提交于 2019-11-30 07:36:17
I have a PowerShell script which installs a patch (contains set of files to be added) on a customer machine. For this, I have created a batch file which executes this PowerShell script. For the customer to run this batch file, the PowerShell script file must be placed onto the customer machine as well. The PowerShell script is in text format, which can be read and understood by the customer easily. Can we convert this script file into some non-readable format (e.g. bin or exe), so that it is not readable by the customer? You can convert the script to Base64 encoding, so that it's not

Colour-coding get-content results

二次信任 提交于 2019-11-30 07:21:27
I've got a powershell script that monitors a log file, filters out the interesting bits and then presents those bits to me as and when they are written to the file. Works wonderfully. The line of interest is: get-content "$logFile" -wait | where { select-string $searchTerm -inp $_ } Now I want to get fancy! I would like the font colour to change everytime a particular term is encountered. I can set the font colour easily enough, but how would you do it on-the-fly with the above statement? Edit: Figured it out, but can't post an answer for 8 hours. Will upload it tomorrow. Try Get-Content

How to capture the exception raised in the scriptblock of start-job?

怎甘沉沦 提交于 2019-11-30 06:41:38
I have the following script, $createZip = { Param ([String]$source, [String]$zipfile) Process { echo "zip: $source`n --> $zipfile" throw "test" } } try { Start-Job -ScriptBlock $createZip -ArgumentList "abd", "acd" echo "**Don't reach here if error**" LogThezippedFile } catch { echo "Captured: " $_ | fl * -force } Get-Job | Wait-Job Get-Job | receive-job Get-Job | Remove-Job However, the exception raised in another powershell instance cannot be captured. What's the best way to capture the exception? Id Name State HasMoreData Location Command -- ---- ----- ----------- -------- ------- 343

Is it possible to create a Database in SQL Server with powershell?

一曲冷凌霜 提交于 2019-11-30 06:39:58
I am trying to create a empty database in SQL server using powershell and SMO but cannot seem to find a way of doing it. Is this possible? Connection script for sql server: [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null $serverName = "localhost" $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') $serverName $server.ConnectionContext.LoginSecure=$false; $credential = Get-Credential $loginName = $credential.UserName -replace("\\","") $server.ConnectionContext.set_Login($loginName); $server.ConnectionContext.set_SecurePassword($credential