1.执行powershell_build_OSharpNS.ps1脚本。
2.如果出现禁止执行脚本,请先以管理员身份运行powershell,然后使用“Set-ExecutionPolicy”命令来设置本机的策略,例如,设置本机策略为“RemoteSigned”;最后再次执行powershell_build_OSharpNS.ps1脚本。
3.附powershell_build_OSharpNS.ps1脚本代码
#region 关键代码:强制以管理员权限运行
$currentWi = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentWp = [Security.Principal.WindowsPrincipal]$currentWi
if( -not $currentWp.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
$x = $MyInvocation.MyCommand.Definition
Start-Process "$psHome\powershell.exe" -ArgumentList "$x " -verb runas
return
}
#endregion
#判断 .NET Core是否安装并且版本>=2.1.301
$DotNetVersion = "2.1.301";
$DotNetInstallerUri = "https://dot.net/v1/dotnet-install.ps1";
###########################################################################
# INSTALL .NET CORE CLI
###########################################################################
Function Remove-PathVariable([string]$VariableToRemove)
{
$path = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($path -ne $null)
{
$newItems = $path.Split(';', [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
[Environment]::SetEnvironmentVariable("PATH", [System.String]::Join(';', $newItems), "User")
}
$path = [Environment]::GetEnvironmentVariable("PATH", "Process")
if ($path -ne $null)
{
$newItems = $path.Split(';', [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { "$($_)" -inotlike $VariableToRemove }
[Environment]::SetEnvironmentVariable("PATH", [System.String]::Join(';', $newItems), "Process")
}
}
# Get .NET Core CLI path if installed.
$FoundDotNetCliVersion = $null;
if (Get-Command dotnet -ErrorAction SilentlyContinue) {
$FoundDotNetCliVersion = dotnet --version;
$FoundDotNetCliVersion = $FoundDotNetCliVersion.Substring(0, 7)
}
#重启电脑
Function IsResetComputer([string]$Value){
if(($Value -eq 'Y') -or ($Value -eq 'y'))
{
Restart-Computer
}
elseif(($Value -eq 'N') -or ($Value -eq 'n'))
{
Exit
}
else{
"输入参数不正确,请重新输入!!!"
$Value = Read-Host
IsResetComputer $Value
}
}
if($FoundDotNetCliVersion -lt $DotNetVersion) {
$FoundDotNetCliVersion
$DotNetVersion
$InstallPath = Join-Path $env:ProgramFiles "dotnet"
if (!(Test-Path $InstallPath)) {
mkdir -Force $InstallPath | Out-Null;
}
(New-Object System.Net.WebClient).DownloadFile($DotNetInstallerUri, "$InstallPath\dotnet-install.ps1");
& $InstallPath\dotnet-install.ps1 -Channel $DotNetChannel -Version $DotNetVersion -InstallDir $InstallPath;
Remove-PathVariable "$InstallPath"
$env:PATH = "$InstallPath;$env:PATH"
"请重启电脑后继续。。。"
$keyValue = Read-Host "是否重启?Y/N"
IsResetComputer $keyValue -ErrorAction Stop
If (!$?)
{
#$Error
"执行出现错误,按任意键退出!!!"
[Console]::Readkey() | Out-Null
Exit
}
}
$env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1
$env:DOTNET_CLI_TELEMETRY_OPTOUT=1
#安装OSharpNS的dotnet new项目模板
#判断是否已安装
Function IsInstall()
{
$FoundTemplate = $null;
if (Get-Command dotnet -ErrorAction Continue) {
$FoundTemplate = dotnet new "OSharp MVC Project" -l
}
If (!$?)
{
dotnet new -i OSharpNS.Template.Mvc_Angular
}
}
IsInstall
"此命令行工具是用来生成项目代码的,一键生成"
$name = Read-Host "请输入项目名称,推荐形如 '公司.项目'的模式"
#write-output $name
dotnet new osharp_sln -n $name
dotnet new osharp_core -n $name -o $name\src\$name.Core
dotnet new osharp_entityconfig -n $name -o $name\src\$name.EntityConfiguration
dotnet new osharp_mvc -n $name -o $name\src\$name.Web
dotnet new osharp_ng -o $name\src\ui
"项目代码生成完成,按任意键退出!"
[Console]::Readkey() | Out-Null
Exit