run powershell script from anywhere

走远了吗. 提交于 2020-06-27 23:07:13

问题


I am currently working on a powershell script. The objective of this script is to import data from a .csv file from which new users are created if that username does not already exist in the Active Directory.

My question is how can I make this script run from any location so all I have to do is type the name of the script and it will run. I have been able to do this in BASH but can't figure out how to do this in power shell. So far google has been little help.

If it makes any difference i'm using Windows Server 2008 R2


回答1:


The basic idea is to create Powershell Function which will do the work (or will call other script placed in other location) and put this method to Profile.ps1 script (the script which is loaded everytime you start powershell) - Look at Windows PowerShell Profiles for further details.




回答2:


The link above for Powershell Function from Tomas Panik is not there anymore so I want to add to the answer here.

Short version:

  1. You can create your function by using Powershell Function. However, this will only last for that session only.
  2. In order for you to use your function regularly, you need to generate/add your function to your own PowerShell profile. Quick tutorial is here. Tomas Panik's link to Windows PowerShell Profiles also has very good info..


来源:https://stackoverflow.com/questions/15466499/run-powershell-script-from-anywhere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!