answerstu

active directory - (Powershell) Ensure variable is interpreted as literal string

I'm working on Powershell scripts to take a selection of AD principal names, pull full DNs for them, and take action on the accounts in question.The active parts are all working fine now, but I've run into an unfortunate problem - several of the AD objects have names that start with $, IE $DUPLICATE-c1870This means that when I output them to a variable, then read that variable in later, it tries to interpret the variable. For the time being I've bodged a fix by manually defining$duplicate='$duplicate'But that A: depends on that being the only s...Read more

loops - Powershell script to rename by parent directory and increment thereafter

I am trying to count the number of files in a directory and then rename the files by parent folder. The first file will be .extension and every file after will be .extension. This is what I have so far:$RootDir = Read-Host -Prompt 'What is the root directory?' $grabFiles = Get-ChildItem $RootDir -Filter *.txt-recurse #count the number of pst files in directory recursively $fileNum = (Get-ChildItem $grabFiles | Measure-Object).Count #Gather parent directory $parent = Resolve-Path . | Split-Path -Leaf for($x=0; x -l...Read more

replication - Bypass the 24 snapshot limitation in set-vmreplication -RecoveryHistory. (powershell,hyperv replica)

It my first question in here,I work in project to automate hyper-v replica solution.so my problem is that in powershell set-vmreplication -RecoveryHistory -RecoveryHistoryhas 24 like a max value and i want to pypass that my need it's 72.i try to see the code source but it's in binary i can't do anythingin that way (#microsoft : * _ *:).i post in microsoft forum and nothingenter image description herethe error (in french) it say 72 > 24 that all.so if someone has a solution or a beginning of solution that will be very helpfull.thank you all.a...Read more

powershell - Launch a program whenever another one is launced

I'm trying to find a way to launch a program whenever another one is launced.For example I want to lauch notepad.exe each time I launch MS Word.There is a way to do that without an always running daemon?Edit: As @RbMm explains Image File Execution Options could be a way, but a recursive call of my program have to be avoided. Maybe use [Kernel32]::CreateProcess with DEBUG_PROCESS flag is a solution, I'm trying to figure it out how to use CreateProcess and avoid the process launch loop as RbMs said.I changed the tag from c++ to Powershell (.ps1 f...Read more

copy - Trying to write a script to find 0kb files, and overwrite them with files from a different drive on the same path - Powershell

I am very new to powershell and trying to write a script that can read all 0kb files in a folder, organize them into a list, and then determine where the same files would be on a different backup drive so that they can be replaced.I have some code that puts it into a list and then attempts to modify the list so that it pulls from the correct drive, however, I cannot get down the file replacement.An example of what I want this to do is this:There is a file at C:\Test\test.txt that is 0kb, empty.The script reads that this file is empty and writes...Read more

PowerShell -eq operator dont return true for date comparison i Azure Function

I am using SharePoint PnP PowerShell to retrieve list items from a SharePoint list. The next thing I want to do is to compare two dates. A date from the received item against today's date. This code works on my local machine. But when I run it on my Azure Function it doesn't return the dates as equal. It doesn't return any errors either.It jsut runs past the if. The code works when I debug it in PowerShell ISE.ForEach ($ListItem in $ListItems){ $title = $ListItem["Title"] $today = [DateTime]::Today $notificationDate = $ListItem[$QMSREV...Read more

powershell - String trim and split

I have a text file that I read and I need to get the values from.Example text file:[Site 01]DBServer=LocalHostDBName=Database01Username=adminPassword=qwerty[Site 02]DBServer=192.168.0.10DBName=Database02Username=adminPassword=qwertyCurrently my code reads through the file and places each each as an array entry for each line DBServer= that is found and this text file can have many sites:$NumOfSites = Get-Content $Sites | Select-String -Pattern "DBServer=" -Context 0,3$i = 0$NumOfSites | ForEach-Object { $svr = $NumOfSites[$i] -i...Read more

powershell - How do I get only directories using Get-ChildItem?

I'm using PowerShell 2.0 and I want to pipe out all the subdirectories of a certain path. The following command outputs all files and directories, but I can't figure out how to filter out the files.Get-ChildItem c:\mypath -RecurseI've tried using $_.Attributes to get the attributes but then I don't know how to construct a literal instance of System.IO.FileAttributes to compare it to. In cmd.exe it would be dir /b /ad /s...Read more

windows server 2008 r2 - PowerShell says "execution of scripts is disabled on this system."

I am trying to run the a .cmd file that calls a PowerShell script from the command prompt, and I am getting the below error: Management_Install.ps1 cannot be loaded because the execution of scripts is disabled on this system.I have ran set-executionpolicy unrestricted and when I run get-executionpolicy from PowerShell I get unrestricted back. //Output from Powershell PS C:\Users\Administrator> get-executionpolicy Unrestricted //Output from DOS C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scr ipts...Read more

Powershell executing .exe file without the folder path

I am fairly new to powershell and I am trying to create a script that executes a .exe file. I can execute them on my machine no problem because the folder path is hard coded. The problem is that if I shift this script to another computer, the .exe it calls might be located in a different folder structure. ExampleMy computer:D:\Folder1\subfolder\RunMe.exeClient computer might beD:\RunMe\subfolder\RunMe.exeI just need it to execute the RunMe.exe no matter where it is. Is there a way to do this in powershell?...Read more

PowerShell Command to Copy File on Remote Machine

I have a requirement to copy file from local machine to remote machine using PowerShell. I can copy the file to remote computer using following command:copy-item -Path d:\Shared\test.txt -Destination \\server1\Sharedthe above command uses network share path to copy the file. I don't want to use network share option as the folder will not be shared on the remote machine. I tried following commands but not working.copy-item -Path d:\Shared\test.txt -Destination \\server1\c$\SharedInvoke-Command -ComputerName \\server -ScriptBlock { copy-item -Pa...Read more

directory - Check for File in multiple directories with an IF statement Powershell

I am facing some problems in powershell. I want to be able to let a powershell command search for multiple directories. With the name being a variable like "$VM_DISK=VM_DISK.vhdx" and let powershell search in that manor so that if that file exists in a folder such as C:\VM_DISK\ it exit the script. I have already tried the "Get-Childitem" but it doesn't seem to work when I put my variable in it. Here is an example: $VM_DISK= "Example.vhdx" $search=Get-ChildItem -Path C:\VM_DISK\* -Filter $VM_DISK -Recurse if ($search -eq $VM_DISK) {wr...Read more

powershell - Delete first n lines in file (.zip file with many "strange" symbols)

This powershell code delete first 4 lines in file(gc "old.zip" | select -Skip 4) | sc "new.zip"But old.zip file has Unix (LF) line endingsAnd this code also converts file line endings to Windows (CR LF) How to delete first 4 lines without converting ?Because of the presence of many "strange" symbols in .zip, other ways to remove the first n lines in a .zip file do not work. For example, more than +4 "old.zip"> "new.zip" in cmd does not work, etc.Through powershell something like it is removed but also not without problems. Do you know othe...Read more