powershell-2.0

Alternative to Append export-csv powershell

元气小坏坏 提交于 2019-12-02 11:56:49
I add user logon and logout tracking script I found that some computers do not export csv as they have powershell 2.0 because append is not supported is there any alternative? $ErrorActionPreference = 'Continue' ####**** Tracking user logon *****##### $username = $env:USERNAME $computername = $env:COMPUTERNAME $ipv4 = Test-Connection -ComputerName (hostname) -Count 1 | foreach { $_.ipv4address } $ipv6 = Test-Connection -ComputerName (hostname) -Count 1 | foreach { $_.ipv6address } $timeformat='MM-dd-yyyy hh:mm:ss tt' $time = (Get-Date).ToString($timeformat) $action = 'Logon' $filedate = 'MM-dd

Export function

不羁的心 提交于 2019-12-02 09:26:01
I am trying to create a function in which a user has to give a filename that can not containt an empty string. Besides that, the string can not contain a dot. When I run this function, I keep looping when I enter "test" for example. Any idea as to why? function Export-Output { do { $exportInvoke = Read-Host "Do you want to export this output to a new .txt file? [Y/N]" } until ($exportInvoke -eq "Y" -or "N") if ($exportInvoke -eq "Y") { do { $script:newLog = Read-Host "Please enter a filename! (Exclude the extension)" if ($script:newLog.Length -lt 1 -or $script:newLog -match ".*") { Write-Host

Picking file names out of a website to download in powershell

岁酱吖の 提交于 2019-12-02 09:18:42
Problem: I'm working on making a PowerShell script that will download the sites source code, find all the file targets, and then download said targets. I'm alright for authentication for the moment, so on my test website, I enabled anonymous authentication, enabled directory browsing, and disabled all other default pages, so all I get is a list of files on my site. What I have so far is this: $source = "http://testsite/testfolder/" $webclient = New-Object system.net.webclient $destination = "c:/users/administrator/desktop/test/" $webclient.downloadstring($source) The $webclient.downloadstring

Running powershell scripts from within a .NET windows app

 ̄綄美尐妖づ 提交于 2019-12-02 08:50:50
问题 I'm needing to run scripts from within a vb.net windows app. I've got the scripts running in the background fine; Using MyRunSpace As Runspace = RunspaceFactory.CreateRunspace() MyRunSpace.Open() Using MyPipeline As Pipeline = MyRunSpace.CreatePipeline() MyPipeline.Commands.AddScript("import-module -name " & moduleName & vbCrLf & "(get-module -name " & moduleName & ").version") Dim results = MyPipeline.Invoke() 'Do something with the results End Using MyRunSpace.Close() End Using However, i

Invoke-Command : A positional parameter cannot be found that accepts argument

不羁的心 提交于 2019-12-02 08:50:40
问题 I am getting the above problem with the below script in powershell. I can't seem to find the issue. The script requires 2 parameters, and I've supplied them on the command line but it still throws an error? Command: PS C:\> powershell.exe -ExecutionPolicy Bypass invoke-command installboot.ps1 -computername 192.168.162.1 -argumentlist MyServer\MyNewService, option2 Script: param( [Parameter(Mandatory=$true)] [string] $IISName, [Parameter(Mandatory=$true)] [string] $installmode ) write-host

Powershell variable export-CSV cache?

柔情痞子 提交于 2019-12-02 08:23:11
I am working on a script by Richard L. Mueller ( http://www.rlmueller.net/PowerShell/PSLastLogon.txt ) Trap {"Error: $_"; Break;} $D = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() $Domain = [ADSI]"LDAP://$D" $Searcher = New-Object System.DirectoryServices.DirectorySearcher $Searcher.PageSize = 200 $Searcher.SearchScope = "subtree" $Searcher.Filter = "(&(objectCategory=person)(objectClass=user))" $Searcher.PropertiesToLoad.Add("samAccountName") > $Null $Searcher.PropertiesToLoad.Add("lastLogon") > $Null $arrUsers = @{} ForEach ($DC In $D.DomainControllers) { $Server =

How to get the next business day in powershell

不打扰是莪最后的温柔 提交于 2019-12-02 08:18:58
I have the following test code. Basically I am checking when a new file is created in folder. I need to know that if the file was created after 4pm display the next business day. Currently my code displays the next day, but I need to display the next business day. Any help would be appreciated. $formatteddate = "{0:h:mm:ss tt}" -f (get-date) if ($formatteddate -gt "4:00:00 PM"){ $(Get-Date).AddDays(1).ToString('MMM d yyyy') } Adding to what jisaak said: "business day" is organization specific. Some organizations don't work on holidays that other organizations do. If you want to handle holidays

Creating Zip files using PowerShell

雨燕双飞 提交于 2019-12-02 08:12:53
I have these below files at a location C:\Desktop\Mobile. Apple_iphone6.dat Apple_iphone7.dat Samsung_edge7.dat Samsung_galaxy.dat Sony_experia.dat Sony_M2.dat I need to create a script that writes the similar files into a single zip. So files Apple_iphone6.dat and Apple_iphone7.dat must be into single zip. So the final zip files created would be: Apple_Files_Timestamp.zip Samsung_Files_Timestamp.zip Sony_Files_Timestamp.zip I tried this Get-ChildItem C:\Desktop\Mobile -Recurse -File -Include *.dat | Where-Object { $_.LastWriteTime -lt $date } | Compress-Archive -DestinationPath C:\Desktop

Invoke-Command : A positional parameter cannot be found that accepts argument

久未见 提交于 2019-12-02 07:14:35
I am getting the above problem with the below script in powershell. I can't seem to find the issue. The script requires 2 parameters, and I've supplied them on the command line but it still throws an error? Command: PS C:\> powershell.exe -ExecutionPolicy Bypass invoke-command installboot.ps1 -computername 192.168.162.1 -argumentlist MyServer\MyNewService, option2 Script: param( [Parameter(Mandatory=$true)] [string] $IISName, [Parameter(Mandatory=$true)] [string] $installmode ) write-host "IISName: " $IISName write-host "Install Mode: " $installmode Try it with the -Command parameter e.g.:

Powershell running as a another user with elevated privileges

情到浓时终转凉″ 提交于 2019-12-02 07:04:59
问题 I have two scripts located in C:\setup: script.ps1 and script1.ps1. I want to be able to run the script1.ps1 from withing script.ps1 as another user and with elevated privileges but I cannot make it work. The new powershell window opens but closes immediately ... here is the script: $cspath = $MyInvocation.MyCommand.Path $sfolder = Split-Path $cspath $spath = Join-Path $sfolder "\Script1.ps1" $sa = "domain\user" $sap = "userpassword" $sasp = ConvertTo-SecureString -String $sap -AsPlainText